我想在帖子中检索评论以进行编辑。我不确定如何处理这个问题。希望我在这里提出正确的问题。
下面'我的 发布 文档是什么样的。
{
"title" : "First Node.js App",
"body" : "testing 123",
"status" : "public",
"user" : "John Doe",
"date" : ISODate("2017-12-21T18:30:09.779Z"),
"comments" : [
{
"commentBody" : "This is awesome! ",
"commentUser" : ObjectId("5a3bfd5a9e65351f9c18ba18"),
"_id" : ObjectId("5a3c02379e65351f9c18ba1a"),
"commentDate" : ISODate("2017-12-21T18:49:27.620Z")
},
{
"commentBody" : "This is second comment.",
"commentUser" : ObjectId("5a3bfd5a9e65351f9c18gt19"),
"_id" : ObjectId("5a3c02379e65351f9c18ba1b"),
"commentDate" : ISODate("2017-12-21T18:49:27.620Z")
}
],
"allowComments" : true
}
如何检索评论ID ObjectId(" 5a3c02379e65351f9c18ba1a")?
我尝试过以下但没有运气。
const post = await Post.findById(req.params.id);
const comment = post.comments.find({"_id": req.params.commentid});
感谢您的帮助!
答案 0 :(得分:2)
您需要将回调传递给Array#find:
const comment = post.comments.find((el) => el._id === req.params.commentid);