我定义了两个模式,一个是user,一个是doc。在文档架构中,我使用createdBy字段来引用用户架构
现在,当我想要显示文档架构中的所有记录时,我无法访问名称
我可以访问doc.education或doc.bio,但无法访问永久文档名称
以下是我尝试过的方法
doc
.find({})
.populate({ path: "createdBy" })
.exec((err, docs) => {
if (!err) {
console.log(docs);
}
});我得到了这个结果
[
{
_id: 5efa9ab935f13d39b01f36e2,
createdBy: {
role: 'doctor',
isVerified: false,
_id: 5ef873fe544ef53874eaacfe,
name: 'drsmira',
email: 'drsmira@gmail.com',
password: '$2a$10$X/52qRFCYA/DwZ6Z1ZftVu2gv.MJ4HsqXrRuB8.ZLbqj.4EVtVmBG',
select: 'Female',
date: 2020-12-31T00:00:00.000Z,
phone: '342343',
city: 'dff',
state: 'dfss',
specifications: [],
__v: 0
},
bio: 'wweqeqwe',
speciality: 'dentist',
education: 'mbbs',
treatment: 'oral care',
location: 'ewrewre',
hospitalList: 'sahyadri',
awards: 'ererewrwe',
fee: '888',
__v: 0,
achievements: ''
}
]现在,我想访问名称字段。我该怎么做?
我试着实现
console.log(docs.createdBy.name),但返回未定义的
发布于 2020-07-03 15:43:12
所以我几乎没有深入研究,感谢vishnu的评论
我使用forEach循环来获得结果
以下是更新后的代码
doc
.find({})
.populate({ path: "createdBy" })
.exec((err, docs) => {
if (!err) {
docs.forEach((el) => {
console.log(el.createdBy.name);
});
res.render("doctors", { list: docs });
}
});https://stackoverflow.com/questions/62710107
复制相似问题