Javascript 带Ref的Mongo架构:未使用保存更新文档

Javascript 带Ref的Mongo架构:未使用保存更新文档,javascript,mongodb,express,mongoose,Javascript,Mongodb,Express,Mongoose,我有一个博客,我正在尝试为用户添加添加评论的功能。我已经参照我的博客模型创建了一个评论模型。当我试图在Postman中将评论添加到博客时,它会将评论保存在评论文档中,但不会将相关的评论添加到我的博客文档中。我希望我的文档中的博客项目有自己的评论字段,每个评论都有 评论模式 const commentSchema = mongoose.Schema({ comment: { type: String, required: true }, blog: { type: mongoose

我有一个博客,我正在尝试为用户添加添加评论的功能。我已经参照我的
博客
模型创建了一个
评论
模型。当我试图在Postman中将评论添加到
博客
时,它会将
评论
保存在评论文档中,但不会将相关的
评论
添加到我的
博客
文档中。我希望我的文档中的博客项目有自己的评论字段,每个评论都有

评论模式

const commentSchema = mongoose.Schema({
  comment: { type: String, required: true },
  blog: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "Blog"
  }
});

commentSchema.set("toJSON", {
  transform: (document, returnedObject) => {
    returnedObject.id = returnedObject._id.toString();
    delete returnedObject._id;
    delete returnedObject.__v;
  }
});

module.exports = mongoose.model("Comment", commentSchema);
const blogSchema = mongoose.Schema({
  title: { type: String, required: true },
  author: { type: String, required: true },
  url: String,
  likes: Number,
  user: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "User"
  },
  comments: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "Comment"
  }
});

blogSchema.set("toJSON", {
  transform: (document, returnedObject) => {
    returnedObject.id = returnedObject._id.toString();
    delete returnedObject._id;
    delete returnedObject.__v;
  }
});
博客模式

const commentSchema = mongoose.Schema({
  comment: { type: String, required: true },
  blog: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "Blog"
  }
});

commentSchema.set("toJSON", {
  transform: (document, returnedObject) => {
    returnedObject.id = returnedObject._id.toString();
    delete returnedObject._id;
    delete returnedObject.__v;
  }
});

module.exports = mongoose.model("Comment", commentSchema);
const blogSchema = mongoose.Schema({
  title: { type: String, required: true },
  author: { type: String, required: true },
  url: String,
  likes: Number,
  user: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "User"
  },
  comments: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "Comment"
  }
});

blogSchema.set("toJSON", {
  transform: (document, returnedObject) => {
    returnedObject.id = returnedObject._id.toString();
    delete returnedObject._id;
    delete returnedObject.__v;
  }
});
我尝试添加新注释的Express代码

blogsRouter.post("/:id/comments", async (request, response, next) => {
  try {
    const blog = await Blog.findById(request.params.id);
    console.log("Blog", blog);
    const comment = new Comment({
      comment: request.body.comment,
      blog: blog._id
    });
    const result = await comment.save();
    console.log("Blog Comments", blog.comments);
    blog.comments = blog.comments.concat(result._id);
    await blog.save();
    response.status(201).json(result.toJSON());
  } catch (error) {
    next(error);
  }
});
在邮递员中提交到该路线时,上述代码会导致以下错误:

TypeError: Cannot read property 'concat' of undefined

如下图所示,评论很好地通过,并保存到评论文档中的数据库中,但没有填充博客文档,因此我想为什么会出现此错误


请注意,我正在为博客填充Get和Put路由中的用户和评论。如果需要,我可以发布,可能有问题

我想你是想在
blogSchema
commentSchema
之间建立一对多的关系。这意味着您需要修改
博客架构中的
注释

const blogSchema = mongoose.Schema({
    title: { type: String, required: true },
    author: { type: String, required: true },
    url: String,
    likes: Number,
    user: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "User"
    },
    comments: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: "Comment"
        }
    ]
});
这将允许您使用
concat
push
如下所示:

blog.comments.push(comment);
结果应该是一样的