2016-12-11 1 views
0

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 
} 

答えて

0

これは奇妙なものでしたが、私は少し厄介な操作でそれを回避することができました。

  1. 同じタイプの新しいスキーマフィールドを作成し、値を に設定します。
  2. すべてのドキュメントを確認して、元のフィールドの をそのフィールドの値に設定します。
0

これはあなたのteam1定義です:

team1: { 
    type: mongoose.Schema.Types.ObjectId, 
    ref: 'Teams' 
} 

、これはあなたですmongoのデータ:

team1: 5835a5f653d4ce23bb33ab19 

ご覧のとおり、team1オブジェクトタイプはObjectIdではありません!それはちょうど普通の文字列です!

モンゴが格納され、このような参照:

team1: { 
    "$ref" : "Teams", 
    "$id" : ObjectId("5835a5f653d4ce23bb33ab19") 
} 

だから、どちらかのmongoにデータを修正したり、あなたのスキームを修正します!

+0

私はあなたが実際に間違っていると思います。それをidに変換するために 'mongoose.mongo.ObjectId(id)'を渡すことはできません。また、私はその正確な方法で参照を設定しています。これらの2つの変数は機能しません。 –

関連する問題