[英]How to Populate on Sub Document's Reference (Mongoose)
How to populate ref document in Sub Document, this my schema: 如何在子文档中填充参考文档,这是我的模式:
var person = mongoose.Schema({
name: { type: [String], index: true },
career: [{
position: { type: mongoose.Schema.Types.ObjectId, ref: 'Orgchart' }
}]
};
var orgchart = mongoose.Schema({
name: { type: [String], index: true },
};
I tried with this part: 我尝试了这一部分:
person.find({ _id: "12345" }).populate('orgchart').exec(function(err, data){
res.send(data);
});
I got error Cannot read property 'name' of undefined
when i call on jade template with 我在调用Jade模板时出现错误,
Cannot read property 'name' of undefined
item.career._orgchart.name
You need to pass the dot-notation pathname of the field to populate to the populate
call: 您需要传递字段的点符号路径名以填充到
populate
调用:
person.find({ _id: "12345" }).populate('career.position').exec(function(err, data){
res.send(data);
});
Not sure why you're trying to access this using _orgchart
from Jade as it's the same position
field within an element of the career
array that will be populated with the referenced orgchart
doc. 不知道为什么要尝试使用Jade的
_orgchart
来访问它,因为它与将使用引用的orgchart
文档填充的career
数组元素中的相同position
字段相同。
Thanks to Mr JohnnyHK 多亏了JohnnyHK先生
I use: 我用:
person.findOne({ _id: req.params.person }).populate('career.position career.grade').exec(function(err, data){
res.send(data);
});
I used item.position.name
in loop data on jade template 我在玉模板上的循环数据中使用了
item.position.name
Thanks 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.