node.js – 使用mongoose填充Model对象的数组

前端之家收集整理的这篇文章主要介绍了node.js – 使用mongoose填充Model对象的数组前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我使用的架构.如您所见,survey_codes模型路径由一个ObjectIds数组组成.
  1. ...
  2. var Schema = mongoose.Schema;
  3. var Email = mongoose.SchemaTypes.Email;
  4. var ObjectId = mongoose.SchemaTypes.ObjectId;
  5.  
  6. var RestaurantSchema = new Schema({
  7. 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}
  8. });
  9.  
  10. var SurveyCodeSchema = new Schema({
  11. code : {type: String,unique: true,valid : {type: Boolean,default: true},create_date : {type: Date,default: Date.now},used_date : {type: Date,default: null}
  12. });@H_404_3@
  13. 这里我正在尝试使用的功能

  14. Restaurant
  15.     .findOne({ id: self.param('id') })
  16.     .populate('survey_codes')
  17.     .exec(function(err,restaurant) {
  18.       if (err)
  19.         console.log('Error in view survey codes function');
  20.       if (!restaurant || restaurant.survey_codes.length < 1)
  21.         self.res.send('No survey codes are yet generated.');
  22.       else
  23.         self.res.send(restaurant.survey_codes);
  24.     });@H_404_3@ 
  25.  

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

  26.   
  27.  
    Locomotive 0.3.7 application starting in development on http://0.0.0.0:3000
  28. /home/pblondin/nodejs-dev/rezerve-locomotive/node_modules/mongoose/lib/utils.js:419
  29.         throw err;
  30.               ^
  31. MissingSchemaError: Schema hasn't been registered for model "function model(doc,fields,skipId) {
  32.     if (!(this instanceof model))
  33.       return new model(doc,skipId);
  34.     Model.call(this,doc,skipId);
  35.   }".@H_404_3@ 
  36.  

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

  37.  

    谢谢!

  38.  

    编辑:

  39.  

    这是一些额外的信息:

  40.  

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

  41.   
  42.  
    [
  43.     {
  44.         "__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": [
  45.             "52617864b9ee6c171b000002","52617864b9ee6c171b000003","52617864b9ee6c171b000004","52617864b9ee6c171b000005","52617864b9ee6c171b000006","52617864b9ee6c171b000007","52617864b9ee6c171b000008","52617864b9ee6c171b000009","52617864b9ee6c171b00000a","52617864b9ee6c171b00000b"
  46.         ]
  47.     }
  48. ]@H_404_3@ 
  49.  

    2)依赖项的版本:

  50.   
  51.  
    mongoose: 3.6.20
  52. mongodb: 1.3.19
  53. locomotive: 0.3.7
  54. locomotive-mongoose: 0.1.0@H_404_3@

解决方法

解决了(!)

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

  1. survey_codes : [{type: ObjectId,@H_404_3@
  2. ref为模型名称,所以改为’SurveyCode’!

猜你在找的Node.js相关文章