2013-06-23 38 views
0

这里是我的架构:猫鼬子填充不工作

var sourcesSchema = { 
    title: String, 
    name: String, 
    url: String, 
    description: String, 
    category: Array, 
    rating: Number, 
    source_pages: [{ 
     type: mongoose.Schema.Types.ObjectId, 
     ref: 'source_page', 
    }] 
} 


var sourcePageschema = { 
    uname: String, 
    source_name: String, 
    page_address: String, 
    driver_name: String, 
    product: { 
     type: mongoose.Schema.Types.ObjectId, 
     ref: 'products' //Edit: I'd put the schema. Silly me. 
    } 
} 

var productsSchema = { 
    title: String, 
    uname: String, 
    descriptin: String, 
    images: Array, 
    currency: String, 
    last_update_time: Number, 
    last_process_time: Number, 
    meta_data: {}, 
    tags: Array, 
    min_price: Number, 
    max_price: Number, 
    prices: [{ 
     type: mongoose.Schema.Types.ObjectId, 
     ref: 'prices' //Edit: I'd put the schema. Silly me. 
    }] 
} 

此代码的工作,并成功地填充source_pages:

_sources.find().populate('source_pages').exec(function (err,sources) { 
    res.json(200, sources); 
}); 

,但如果我要填充的产品太:

_sources.find().populate('source_pages').populate('source_pages.product').exec(function (err,sources) { 
    res.json(200, sources); 
}) 

此错误:

TypeError: Cannot call method 'path' of undefined at search (/home/sina/rhino2/node_modules/mongoose/lib/model.js:2088:28) at search (/home/sina/rhino2/node_modules/mongoose/lib/model.js:2107:22) at Function._getSchema (/home/sina/rhino2/node_modules/mongoose/lib/model.js:2114:5) at populate (/home/sina/rhino2/node_modules/mongoose/lib/model.js:1719:22) at Function.Model.populate (/home/sina/rhino2/node_modules/mongoose/lib/model.js:1702:5) at cb (/home/sina/rhino2/node_modules/mongoose/lib/query.js:1690:11) at /home/sina/rhino2/node_modules/mongoose/lib/utils.js:414:16 at /home/sina/rhino2/node_modules/mongoose/node_modules/mongodb/lib/mongodb/cursor.js:158:16 at commandHandler (/home/sina/rhino2/node_modules/mongoose/node_modules/mongodb/lib/mongodb/cursor.js:643:16) at null. (/home/sina/rhino2/node_modules/mongoose/node_modules/mongodb/lib/mongodb/db.js:1641:20)

回答

2

我只是在追查同样的问题,我相信你在找的是这个Mongoose: deep population (populate a populated field)

基本上,你不能做你正在做的事情,除非你在你的回调函数中做了,然后将它插入到你的回报中。我试图避免这种情况,但目前看来这是唯一的选择。另一种选择是,如果你打算做很多这种类型的东西,就要使用关系数据库进行研究。

+0

我用异步nodejs模块修复了这个问题 –

+0

谢谢,我也会研究这个模块! – abritez