【发布时间】:2018-01-06 12:18:02
【问题描述】:
目前有幻灯片和不同类型幻灯片的概念。我正在尝试在幻灯片对象上填充 slides 数组,并将动态引用设置为幻灯片类型,以获取具有不同文档的填充幻灯片数组。
匹配 http://mongoosejs.com/docs/populate.html 标题“动态引用”下的文档,我的代码如下所示。
// Abreviated slideshow model
var slideshowSchema = mongoose.Schema({
slides: [{
slideType: String,
item: {
type: Schema.Types.ObjectId,
refPath: 'slides.slideType'
}
}]
}]
// controller get route
app.get('/slideshow/:id', function(req, res, next) {
var id = req.params.id;
if(id){
Slideshow
.findById(id)
.populate('slides.item')
.exec(function(err, slideshow) {
res.writeHead( 200, { "Content-Type": "application/json" } );
res.end(JSON.stringify(slideshow));
});
}
else{
return next();
}
});
// type of slide trying to be populated
var currentWeatherSchema = mongoose.Schema({
slideType: {
type: String,
required: true
},
slideshowId: {
type: Schema.Types.ObjectId,
required: true
}
// other unique props
});
在硬编码的 slideType 上进行直接填充时,幻灯片按预期填充,并且在运行上述代码时,我确实得到了响应,并且幻灯片数组填充了正确数量的对象,但它们看起来像这样预期文件:
"slides": [
{
"_bsontype": "ObjectID",
"id": {
"type": "Buffer",
"data": [
89,
126,
152,
150,
243,
157,
179,
147,
165,
23,
247,
56
]
}
}
]
我已经无数次阅读这些示例,但无法弄清楚我哪里出错了。有没有人有这样做的经验,或者看看我可能缺少什么?
【问题讨论】:
标签: node.js mongoose mongoose-schema mongoose-populate