Node.js 如何使用.populate填充此博客文章下的评论
我在post请求响应中使用ObjectID ref.创建了模型。它填充了帖子下的评论,但对get请求不起作用。请问我错过了什么Node.js 如何使用.populate填充此博客文章下的评论,node.js,mongodb,express,router,mongoose-populate,Node.js,Mongodb,Express,Router,Mongoose Populate,我在post请求响应中使用ObjectID ref.创建了模型。它填充了帖子下的评论,但对get请求不起作用。请问我错过了什么 router.get('/:id', async (req, res) => { try { const post = await Post.findById(req.params.id) .populate("comments") res.send(post) } catch (err
router.get('/:id', async (req, res) => {
try {
const post = await Post.findById(req.params.id)
.populate("comments")
res.send(post)
} catch (error) {
console.log(error)
res.send(error)
}
});
这是我得到的回应:它没有显示评论
{
"comments": [],
"_id": "5f9d2cf2f851f6214cff96e6",
"title": "My First Blog Post",
"post": "This is the content as Edited",
"user": "5f9d26bf5cf5e91a6441b0e3",
"createdAt": "2020-10-31T09:22:58.453Z",
"updatedAt": "2020-10-31T09:26:01.685Z",
"__v": 0
}
是否确实已将新注释添加到注释集合?我的意思是,如果它没有填充,它至少应该返回注释的ID。也请放入架构代码。是否确实已将新注释添加到注释集合?我的意思是,如果它没有填充,它至少应该返回注释的ID。也要把你的模式代码放进去。