猫鼬使用方法将模式添加到模式中
Mongoose add schema into schema using method
我正在尝试构建一个使用 Node.js 作为服务器端语言和 Mongo 数据库的博客。
所以在我的博客中,人们可以像其他博客一样撰写 post 并向 post 添加评论。
每个 post 都会有评论,这就是我正在尝试使用我的 post 架构。当有人试图在 post 上写评论时,它会向服务器发送一个 POST 请求,创建一个新评论并将其保存到数据库中。
当我测试时,评论本身被正确保存,但我无法将其附加到 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 模式有评论数组来存储评论。每次用户添加评论时,都会创建一个 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请求会寻找post添加评论,保存评论,然后调用addComment()将评论拼接成[=45=的评论数组].
此处创建的评论将用作 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
因为在您的 post 模型中
comments: [
{
comment: {
type: mongoose.Schema.Types.ObjectId, // you are storing ObjectId here
ref: "Comment"
}
}
]
因此,您可以将您的评论存储为您的评论 ID 数组,然后当您想要填充它时您可以获得完整的评论
我正在尝试构建一个使用 Node.js 作为服务器端语言和 Mongo 数据库的博客。
所以在我的博客中,人们可以像其他博客一样撰写 post 并向 post 添加评论。
每个 post 都会有评论,这就是我正在尝试使用我的 post 架构。当有人试图在 post 上写评论时,它会向服务器发送一个 POST 请求,创建一个新评论并将其保存到数据库中。
当我测试时,评论本身被正确保存,但我无法将其附加到 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 模式有评论数组来存储评论。每次用户添加评论时,都会创建一个 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请求会寻找post添加评论,保存评论,然后调用addComment()将评论拼接成[=45=的评论数组]. 此处创建的评论将用作 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
因为在您的 post 模型中
comments: [
{
comment: {
type: mongoose.Schema.Types.ObjectId, // you are storing ObjectId here
ref: "Comment"
}
}
]
因此,您可以将您的评论存储为您的评论 ID 数组,然后当您想要填充它时您可以获得完整的评论