提问人:Temitope Sam 提问时间:9/19/2023 最后编辑:Temitope Sam 更新时间:9/19/2023 访问量:41
我正在为我的博客应用程序的嵌套评论功能而苦苦挣扎
I'm struggling with nested comment feature for my blog app
问:
因此,我正在开发一个博客的后端,用户可以在其中与帖子进行交互。我正在尝试实现一种评论功能,其中评论可以具有尽可能多的嵌套回复。
以下是我的架构:
品
`const articleSchema = new mongoose.Schema({
title: {
type: String,
required: true,
},
content: {
type: String,
required: true,
},
media: [
{
public_id: {
type : String,
required : true
},
asset_id: {
type: String,
required: true
},
secure_url: {
type: String,
required: true
},
original_filename: {
type: String,
required: true
}
}
],
author: {
type: String,
// type: mongoose.Schema.Types.ObjectId,
// ref: "User",
// required: true
},
comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }],
status: {
type: String,
required: true,
default: "submitted",
enum: ["submitted", "under review", "rejected", "edits required", "approved"]
},
category: [categorySchema], // An array of category documents
},`
评论
`const commentSchema = new Schema({
content: { type: String, required: true },
author: { type: String, required: true }, //{ type:
Schema.Types.ObjectId, ref: "User" },
post: { type: Schema.Types.ObjectId, ref: "Article" },
likes: Number,
replies: [this],
}, { timestamps: true });`
所以我有一个评论控制器可以帮助创建评论并且工作正常。我创建了一个回复控制器,它可以很好地将回复添加到已创建的评论中,但不适用于其他嵌套回复。
这是创建注释的函数:
`async createComment(req, res){
try{
const post = await Article.findById(req.params.postId);
if(!post){
throw new Error("Article Not Found!");
}
const comment = new Comment({
content: req.body.content,
author: "testUser", //req.user.id,
post: req.params.postId
});
await comment.save();
post.comments.push(comment);
await post.save();
return res.redirect(`/articles/${req.params.postId}/`)
} catch(error){
console.log(error);
return res.send(error);
}
}`
这是创建回复的初始函数:
`async createReply(req, res){
try{
const { postId, commentId } = req.params;
const post = await Article.findById(postId);
if(!post){
throw new Error("Article not found!");
} else {
const foundComment = post.comments.find(comment => comment._id.toString() === commentId);
if (foundComment){
const reply = await new Comment({
content: req.body.content,
author: "testUser", //req.user.id,
post: req.params.postId,
}).save();
foundComment.replies.push(reply);
await foundComment.save();
return res.redirect(`/articles/${postId}/`);
}
throw new Error("Comment not found!");
}
} catch(error){
console.log(error);
return res.send(error);
}
}`
这个特定的 createReply 函数只会添加对评论的回复,而不会对其他嵌套回复添加回复。所以我想修补一下,想出了这样的东西:
`async createReply(req, res){
try{
const { postId, commentId } = req.params;
const post = await Article.findById(postId);
if(!post){
throw new Error("Article not found!");
} else {
const parentComment = await findReplies(post.comments, commentId);
if (parentComment){
const reply = await new Comment({
content: req.body.content,
author: "testUser", //req.user.id,
post: req.params.postId,
}).save();
parentComment.replies.push(reply);
await parentComment.save();
return res.redirect(`/articles/${postId}/`);
}
throw new Error("Comment not found!");
}
} catch(error){
console.log(error);
return res.send(error);
}
}`
这是 findReplies 函数:
`async function findReplies(comments, CommentId) {
for (const comment of comments) {
// Check if the current comment matches the desired parentCommentId
if (comment._id.toString() === CommentId) {
return comment; // Return the Mongoose model instance
}
// Recursively search for replies within this comment's replies
if (comment.replies && comment.replies.length > 0) {
const nestedReplies = await findReplies(comment.replies, CommentId);
if (nestedReplies) {
return nestedReplies; // Return the Mongoose model instance
}
}
}
}`
从错误消息中,我可以看出 findReplies 函数不返回模型,而只返回 javascript 对象。
这是我获取文章时的结果:
`{
"post": {
"_id": "65083c03e13ccc61e42865fa",
"title": "test article",
"content": "Get some coconut, rice, pepper...",
"media": [
{
"public_id": "Foodblog9ja/Media/1695043027956_1",
"asset_id": "e97d2127ca2ebf503295a9589146f9e2",
"secure_url": "https://res.cloudinary.com/foodblog9ja/image/upload/v1695043029/Foodblog9ja/Media/1695043027956_1.jpg",
"original_filename": "wfh1",
"_id": "65084dd700faf33389c5c79f"
},
{
"public_id": "Foodblog9ja/Media/1695043027953_0",
"asset_id": "1a9c22ee5da52f328f2248a4e26bd5ac",
"secure_url": "https://res.cloudinary.com/foodblog9ja/image/upload/v1695043030/Foodblog9ja/Media/1695043027953_0.jpg",
"original_filename": "wfh3",
"_id": "65084dd700faf33389c5c7a0"
}
],
"author": "test",
"comments": [
{
"_id": "65084f7000faf33389c5c7a6",
"content": "This is a job well done... can I write for you please?",
"author": "testUser",
"post": "65083c03e13ccc61e42865fa",
"replies": [
{
"content": "thank you so much... Mail me, let's discuss writing for us.",
"author": "testUser",
"post": "65083c03e13ccc61e42865fa",
"replies": [],
"_id": "650853d3d23e68f542076bc2",
"createdAt": "2023-09-18T13:42:43.577Z",
"updatedAt": "2023-09-18T13:42:43.577Z",
"__v": 0
}
],
"createdAt": "2023-09-18T13:24:00.487Z",
"updatedAt": "2023-09-18T13:42:43.590Z",
"__v": 1
}
],
"status": "submitted",
"category": [
{
"name": "food",
"subcategories": [
"african food"
],
"_id": "65083c03e13ccc61e42865fd"
}
],
"createdAt": "2023-09-18T12:01:07.810Z",
"updatedAt": "2023-09-18T13:24:00.565Z",
"__v": 3
}
}`
该人确认他发送了邮件的其他回复不会反映在这里,即使该邮件的记录已在数据库中创建。
答: 暂无答案
评论