2016-10-15 4 views
0

私は、既存のアプリケーションのためにバッジ "1234567"を持つユーザーを作成できるように、以下のスキーマを持っています。私はmaxlengthバリデーションの検証エラーb/cを受け取ることを期待していました。モンゴースアレイオブジェクトmaxlengthバリデーションが動作しません

私のスキーマには何か問題がありますか?

module.exports = function(db) { 
    var mongoose = require('mongoose'); 

    var appSchema = mongoose.Schema({ 
     name: { 
      type: String, 
      required: true, 
      unique: true, 
     }, 
     settings: mongoose.Schema.Types.Mixed, 
     user: [{ 
      badge: { 
      type: String, 
      minlength: 5, 
      maxlength: 5 
      } 
     }] 
    }); 

    // Export the schema for the app to use 
    return db.model('apps', appSchema); 
} 

私はアプリのドキュメント内の配列に新しいユーザレコードを追加する

apps.findByIdAndUpdate(req.params.id, {$push: {user: req.body.user}}, {runValidators: true},callback()); 

を使用しようとしています。

答えて

0

ドキュメントから、mongoose requires an Schema to validate nested objects

あなたのスキーマはそのように定義する必要があります。

代わりに次の定義を使用できますか?

var userSchema = mongoose.Schema({ 
    badge: { 
     type: String, 
     minlength: 5, 
     maxlength: 5 
    } 
}); 

var appSchema = mongoose.Schema({ 
    name: { 
     type: String, 
     required: true, 
     unique: true, 
    }, 
    settings: mongoose.Schema.Types.Mixed, 
    user: [userSchema] 
}); 
+0

まだ動作していないようです。これは私が既存のアプリケーションにユーザーレコードを挿入しようとしているところです: 'apps.findByIdAndUpdate(req.params.id、{$ push:{user:req.body.user}}、{runValidators:true}、コールバック()); ' – Catfish

関連する問題