使用mongoose填充一组Model对象

pbl*_*din 7 mongoose node.js

这是我使用的架构.如您所见,survey_codes模型路径由一个ObjectIds数组组成.

...
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}
});
Run Code Online (Sandbox Code Playgroud)

这里我正在尝试使用的功能:

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);
    });
Run Code Online (Sandbox Code Playgroud)

当我执行该函数时,它给了我这个错误:

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);
  }".
Run Code Online (Sandbox Code Playgroud)

我无法理解这一点.这是我第一次在这里发帖,我注意到你们中的几个人回答了类似的问题,但解决方案并不适用于我的情况.

谢谢!

编辑:

这是一些额外的信息:

1)来自餐馆集合的样本:

[
    {
        "__v": 1,
        "_id": "52617861b9ee6c171b000001",
        "id": "AAA",
        "name": "Name",
        "owner_name": "Owner",
        "reservation_email": "email@new.com",
        "survey_url": "new@new.com",
        "created_at": "2013-10-18T18:05:21.447Z",
        "survey_codes": [
            "52617864b9ee6c171b000002",
            "52617864b9ee6c171b000003",
            "52617864b9ee6c171b000004",
            "52617864b9ee6c171b000005",
            "52617864b9ee6c171b000006",
            "52617864b9ee6c171b000007",
            "52617864b9ee6c171b000008",
            "52617864b9ee6c171b000009",
            "52617864b9ee6c171b00000a",
            "52617864b9ee6c171b00000b"
        ]
    }
]
Run Code Online (Sandbox Code Playgroud)

2)依赖项的版本:

mongoose: 3.6.20
mongodb: 1.3.19
locomotive: 0.3.7
locomotive-mongoose: 0.1.0
Run Code Online (Sandbox Code Playgroud)

pbl*_*din 7

解决了(!)

在我的模型中一个简单的拼写错误:

survey_codes          : [{type: ObjectId, ref: SurveyCode}],
Run Code Online (Sandbox Code Playgroud)

ref为模型名称,所以改为'SurveyCode'!