Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何使用.populate填充此博客文章下的评论_Node.js_Mongodb_Express_Router_Mongoose Populate - Fatal编程技术网

Node.js 如何使用.populate填充此博客文章下的评论

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

我在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 (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。也要把你的模式代码放进去。