简体   繁体   English

如何在子文档的引用上填充(猫鼬)

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM