const PostSchema = new mongoose.Schema({
author: {
type: String,
required: true,
},
title: {
type: String,
required: true
},
description: {
type: String,
required: true
},
})
const CommentSchema = new mongoose.Schema({
postId: {
type: ObjectID,
ref: 'posts',
required: true,
},
parentCommentId: {
type: ObjectID,
ref: 'comments',
required: false, // if not populated, then its a top level comment
},
username: {
type: String,
required: true,
},
detail: {
type: String,
required: true,
},
})
const Post = mongoose.model('Post', PostSchema)
const Comment = mongoose.model('Comment', CommentSchema)
这是我的猫鼬模式。 基本上有一个帖子和评论别人都可以回复评论。
例如。
[A Post]
[A Comment]
[A reply to the above comment]
[A reply to the above comment]
[A Comment]
[A Comment]
我在用express和mongo命令查询这一点时遇到困难
如果给出postId,我想要检索一个注释列表,这样
[{
"_id": "5f2e6efad8c72e0fb08c4f91",
"postId": "5f2e6df2186496186c2bf9cb",
"username": "Shaahid",
"detail": "detail1",
"__v": 0,
"childComment:" [{
"_id": "5f2e6ee1d8c72e0fb08c4f8f",
"postId": "5f2e6df2186496186c2bf9cb",
"username": "Bob",
"detail": "detail1",
"parentCommentId": "5f2e6dfb186496186c2bf9cc",
"__v": 0}]
}]
上述内容基本上
[post]
[comment]
[a reply to above comment]
我的尝试
// A route to get all comments given post id
app.get("comments/:id", (req, res) => {
const id = req.params.id
if (!ObjectID.isValid(id)) {
res.status(404).send()
return
}
Comment.find({ postId: id }).then((comment) => {
if (!comment) {
res.status(404).send('Resource not found')
} else {
comment.filter(comm => {
Comment.find({ parentCommentId: comm.id }).then((reply) => {
comm.childComment = reply
})
})
res.send(comment)
}
})
.catch((error) => {
log(error)
res.status(500).send('Internal Server Error') // server error
})
});
上面的代码不起作用,我不确定为什么。 我首先在给定的帖子ID下找到所有的评论。 然后过滤该列表,检查是否存在子注释,并将其添加到对象中,然后发送JSON。 相反,由于某种原因,它没有添加列表,我认为这与异步有关。 无论如何都要实现这一点?
如果您正确地看到下面代码
comment.filter((comm) => {
Comment.find({ parentCommentId: comm.id }).then((reply) => {
comm.childComment = reply;
});
});
res.send(comment);
您正在循环中执行异步comment.find
查询,因此nodejs是非阻塞的,不会等待完成此操作。
另外,如果我能正确地看到,您已经从comment.find({postid:id})
中获取了特定帖子的所有评论,因此我们不需要从这里的数据库中获取更多的数据来获得所需的结果。
let comments = [{
postId: "123456789",
_id: "1",
comment: "1 comment",
username: "xyz",
parentCommentId: null,
},{
postId: "123456789",
_id: "2",
comment: "2 comment",
username: "xyz",
parentCommentId: null,
},{
postId: "123456789",
_id: "1.1",
comment: "1.1 comment",
username: "xyz",
parentCommentId: "1",
},{
postId: "123456789",
_id: "1.2",
comment: "1.2 comment",
username: "xyz",
parentCommentId: "1",
},{
postId: "123456789",
_id: "1.1.1",
comment: "1.1.1 comment",
username: "xyz",
parentCommentId: "1.1",
}];
我们可以在本地操作数据,以获得所需的注释分层视图(父子关系)。 代码如下所示
null
let comments = [{
postId: "123456789",
_id: "1",
comment: "1 comment",
username: "xyz",
parentCommentId: null,
},{
postId: "123456789",
_id: "2",
comment: "2 comment",
username: "xyz",
parentCommentId: null,
},{
postId: "123456789",
_id: "1.1",
comment: "1.1 comment",
username: "xyz",
parentCommentId: "1",
},{
postId: "123456789",
_id: "1.2",
comment: "1.2 comment",
username: "xyz",
parentCommentId: "1",
},{
postId: "123456789",
_id: "1.1.1",
comment: "1.1.1 comment",
username: "xyz",
parentCommentId: "1.1",
}];
let heirarchical_comments_view = {}, parent_comments_id = {}, filtered_view = [];
for(let i=0;i<comments.length;i++){
if(!comments[i].parentCommentId){
parent_comments_id[comments[i]._id] = true;
heirarchical_comments_view[comments[i]._id] = {...comments[i], child_comments: []};
}else{
heirarchical_comments_view[comments[i]._id] = {...comments[i], child_comments: []};
heirarchical_comments_view[comments[i].parentCommentId].child_comments.push(heirarchical_comments_view[comments[i]._id]);
}
}
for(const x in heirarchical_comments_view){
if(parent_comments_id[x]){
filtered_view.push(heirarchical_comments_view[x]);
}
}
console.log(filtered_view);