2013-10-18 10 views
6

これは私が使用するスキーマです。ご覧のように、survey_codesモデルパスはObjectIdの配列で構成されています。モデルオブジェクトの配列にマングースを埋め込む

... 
var Schema = mongoose.Schema; 
var Email = mongoose.SchemaTypes.Email; 
var ObjectId = mongoose.SchemaTypes.ObjectId; 

var RestaurantSchema = new Schema({ 
    id     : {type: String, required: true, unique: true}, 
    name     : {type: String, required: true}, 
    owner_name   : String, 
    reservation_email  : Email, 
    survey_url   : String, 
    survey_codes   : [{type: ObjectId, ref: SurveyCode}], 
    created_at   : {type: Date, default: Date.now} 
}); 

var SurveyCodeSchema = new Schema({ 
    code     : {type: String, unique: true, required: true}, 
    valid     : {type: Boolean, default: true}, 
    create_date   : {type: Date, default: Date.now}, 
    used_date    : {type: Date, default: null} 
}); 

そして、ここで私が使用しようとしている機能:

Restaurant 
    .findOne({ id: self.param('id') }) 
    .populate('survey_codes') 
    .exec(function(err, restaurant) { 
     if (err) 
     console.log('Error in view survey codes function'); 
     if (!restaurant || restaurant.survey_codes.length < 1) 
     self.res.send('No survey codes are yet generated.'); 
     else 
     self.res.send(restaurant.survey_codes); 
    }); 

私は関数を実行していた場合、それは私に、このエラーを与える:

Locomotive 0.3.7 application starting in development on http://0.0.0.0:3000 

/home/pblondin/nodejs-dev/rezerve-locomotive/node_modules/mongoose/lib/utils.js:419 
     throw err; 
      ^
MissingSchemaError: Schema hasn't been registered for model "function model(doc, fields, skipId) { 
    if (!(this instanceof model)) 
     return new model(doc, fields, skipId); 
    Model.call(this, doc, fields, skipId); 
    }". 

私は単に」することができますこの頭の上に頭を浮かべてください。これは私がここに投稿した初めての話ですが、私は同じ質問にお答えいただいた皆さんにお会いしたことがありますが、私の場合は解決していません。

ありがとうございます!

EDITED:ここ

は、いくつかの追加情報です:

1)レストランのコレクションからのサンプル:

[ 
    { 
     "__v": 1, 
     "_id": "52617861b9ee6c171b000001", 
     "id": "AAA", 
     "name": "Name", 
     "owner_name": "Owner", 
     "reservation_email": "[email protected]", 
     "survey_url": "[email protected]", 
     "created_at": "2013-10-18T18:05:21.447Z", 
     "survey_codes": [ 
      "52617864b9ee6c171b000002", 
      "52617864b9ee6c171b000003", 
      "52617864b9ee6c171b000004", 
      "52617864b9ee6c171b000005", 
      "52617864b9ee6c171b000006", 
      "52617864b9ee6c171b000007", 
      "52617864b9ee6c171b000008", 
      "52617864b9ee6c171b000009", 
      "52617864b9ee6c171b00000a", 
      "52617864b9ee6c171b00000b" 
     ] 
    } 
] 

2)は、依存関係のバージョン:解決しよう

mongoose: 3.6.20 
mongodb: 1.3.19 
locomotive: 0.3.7 
locomotive-mongoose: 0.1.0 
+1

私は、モデルオブジェクトの参照ではなく、モデルの名前であるべきだと思います。例えば ​​'ref:" SurveyCode "(それがその名前の場合)。 –

+0

それだけです!このタイプミスは私に大きな頭痛を与えました。 – pblondin

答えて

5

( !)

私のモデルで

シンプルタイプミス:ので、モデル名するよう

survey_codes   : [{type: ObjectId, ref: SurveyCode}], 

refの 'SurveyCode'代わりに!

+1

こんにちはpblondin、私は同様の質問を掲載しました。 [リンク](http://stackoverflow.com/questions/27565290/populate-in-mongoose/27684986#27684986) – Cauca

関連する問題