Mongooseで単純な参照フィールドを設定しようとすると大きな問題が生じます。 次のエラーが発生します。私が知る限り、実際の検証エラーはありません。MongooseError:参照を設定するときにプロパティ 'options'を未定義に読み取ることができません
'contents.0.modules.0.matches.0.':
{ MongooseError: Cannot read property 'options' of undefined
at ValidatorError (C:\Users\Simon\Documents\Projects\eventvods\node_modules\mongoose\lib\error\validator.js:24:11)
at _init (C:\Users\Simon\Documents\Projects\eventvods\node_modules\mongoose\lib\document.js:372:37)
...
at init (C:\Users\Simon\Documents\Projects\eventvods\node_modules\mongoose\lib\document.js:348:7)
at model.Document.init (C:\Users\Simon\Documents\Projects\eventvods\node_modules\mongoose\lib\document.js:313:3)
message: 'Cannot read property \'options\' of undefined',
name: 'ValidatorError',
properties: [Object],
kind: 'cast',
path: undefined,
value: undefined } } }
保存に失敗したので、
var matchSchema = new Schema({
team1: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Teams'
},
team2: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Teams'
},
team1_2: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Teams'
},
team2_2: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Teams'
},
...
});
var moduleSchema = new Schema({
matches: [matchSchema],
...
});
var sectionSchema = new Schema({
modules: [moduleSchema],
...
});
サンプルオブジェクトのようなマングーススキーマ:
{
team1: 5835a5f653d4ce23bb33ab19,
team2: 5835a70353d4ce23bb33ab21
}
私はあなたが実際に間違っていると思います。それをidに変換するために 'mongoose.mongo.ObjectId(id)'を渡すことはできません。また、私はその正確な方法で参照を設定しています。これらの2つの変数は機能しません。 –