我正在尝试构建一个使用 Node.js 作为服务器端语言和 Mongo DB 的博客。
因此,在我的博客中,人们可以像其他博客一样撰写帖子并向帖子添加评论。
每个帖子都会有评论,这就是我在帖子架构中尝试的内容。当有人尝试在帖子上发表评论时,它会向服务器发送 POST 请求,创建新评论并将其保存到数据库中。
当我测试时,评论本身已正确保存,但我在将其附加到帖子架构中时遇到问题。
这是我的 postSchema.js:
const mongoose = require("mongoose");
const postSchema = new mongoose.Schema(
{
owner: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
required: true
},
title: {
type: String,
required: true
},
body: {
type: String,
required: true
},
comments: [
{
comment: {
type: mongoose.Schema.Types.ObjectId,
ref: "Comment"
}
}
]
}
);
还有我的 commentSchema.js:
const mongoose = require("mongoose");
const commentSchema = new mongoose.Schema(
{
owner: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
required: true
},
post: {
type: mongoose.Schema.Types.ObjectId,
ref: "Post",
required: true
},
content: {
type: String,
required: true
}
}
);
module.exports = Comment = mongoose.model("Comment", commentSchema);
所以帖子模式有评论数组来存储评论。每次用户添加评论时,都会创建一个 POST 请求:
router.post("/:id/new_comment", async (req, res) => {
const { content } = req.body;
const comment = new Comment({
content,
owner: req.user._id,
post: req.params.id
});
try {
const post = await Post.findOne({ _id: req.params.id });
await comment.save();
await post.addComment(comment);
res.status(201).send({ post, comment });
} catch (error) {
res.status(500).send({ error: error.message });
}
});
因此这个 POST 请求将查找要添加评论的帖子,保存评论,并调用 addComment() 将评论连接到帖子的 comments 数组中。 此处创建的注释将用作 addComment() 函数的参数。
addComment() 函数位于 PostSchema.js 文件内,它是:
postSchema.methods.addComment = async function(comment) {
const post = this;
post.comments = post.comments.concat({ comment });
await post.save();
return post;
};
一切正常,但结果与我预想的不同。 我预计它会像
{
"post": {
"_id": "5e2e94c9e6ef8100ecfaf665",
"title": "Test Post",
"body": "Test Body",
"owner": "5e2e6bbd18929829e8679cec",
"comments": [
{
"_id": "5e2e98d9587c78444cd600b2",
"comment": {
"_id": "5e2e98d9587c78444cd600b1",
"content": "Test Comment",
"owner": "5e2e6bbd18929829e8679cec",
"post": "5e2e94c9e6ef8100ecfaf665",
"__v": 0
}
}
],
},
"comment": {
"_id": "5e2e98d9587c78444cd600b1",
"content": "Test Comment",
"owner": "5e2e6bbd18929829e8679cec",
"post": "5e2e94c9e6ef8100ecfaf665",
"__v": 0
}
}
第一个看起来不错,但当我尝试保存第二个评论时,问题就出现了。 之前保存的评论以某种方式更改,如下所示:
{
"post": {
"_id": "5e2e94c9e6ef8100ecfaf665",
"title": "Test Post",
"body": "Test Body",
"owner": "5e2e6bbd18929829e8679cec",
"comments": [
{
"_id": "5e2e98d9587c78444cd600b2",
"comment": "5e2e98d9587c78444cd600b1"
},
{
"_id": "5e2e98d9587c78444cd600b3",
"comment": {
"_id": "5e2e98d9587c78444cd600b2",
"content": "Test Comment 2",
"owner": "5e2e6bbd18929829e8679cec",
"post": "5e2e94c9e6ef8100ecfaf665",
"__v": 0
}
}
],
},
"comment": {
"_id": "5e2e98d9587c78444cd600b2",
"content": "Test Comment 2",
"owner": "5e2e6bbd18929829e8679cec",
"post": "5e2e94c9e6ef8100ecfaf665",
"createdAt": "2020-01-27T08:01:29.492Z",
"updatedAt": "2020-01-27T08:01:29.492Z",
"__v": 0
}
}
当我创建测试评论 2 时,我保存的第一条评论发生了变化。什么可能会导致这种情况? 感谢您阅读这么长的问题。 任何帮助将不胜感激!祝你有美好的一天。
最佳答案
问题在于您传递的是完整评论而不是其 ID。
await post.addComment(comment_id); // pass comment id here
因为在你的帖子模型中
comments: [
{
comment: {
type: mongoose.Schema.Types.ObjectId, // you are storing ObjectId here
ref: "Comment"
}
}
]
因此,您可以将您的评论存储为评论 ID 的数组,然后当您想要填充它时可以获得完整的评论
关于javascript - Mongoose 使用方法将模式添加到模式中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59927992/