如何在Mongoose中推送数组中的对象(错误)

时间:2017-12-09 12:54:30

标签: node.js database mongodb mongoose nosql

我正在使用Mongoose和Mongo在Node服务器上创建一个路由,它将注释存储在blogPost的comments数组中(我将发布模型代码)。当我尝试执行查询时,它给出了以下错误: Postman error
这些是我的模型和路线:

blogPost模型

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const BlogPostSchema = new Schema({
  content: {
    type: String,
    validate: {
      validator: (content) => content.length > 5,
      message: 'Content must contain at least 6 characters.'
    },
    required: [true, 'Content must be filled in.']
  },
  rating: Number,
  title: String,
  user: { type: Schema.Types.ObjectId, ref: 'user' },
  board: {type: Schema.Types.ObjectId, ref: 'board'},
  comments: [{
    type: Schema.Types.ObjectId,
    ref: 'comment'
  }]
});

const BlogPost = mongoose.model('blogPost', BlogPostSchema);

module.exports = BlogPost;

评论模型

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const CommentSchema = new Schema({
  content: {
    type: String,
    validate: {
      validator: (content) => content.length > 5,
      message: 'Content must contain at least 6 characters.'
    },
    required: [true, 'Content must be filled in.']
  },
  user: { type: Schema.Types.ObjectId, ref: 'user' },
  rating: Number

  // board: Board
});

// UserSchema.virtual('postCount').get(function(){
//   return this.posts.length;
// });

const Comment = mongoose.model('comment', CommentSchema);

module.exports = Comment;

路线

routes.put('/blogPosts/:id/comment', function(req, res) {
    const blogPostId = req.param('id');
    const commentProps = req.body;

    BlogPost.findById(req.params.id)
        .then((blogPost) => {
          blogPost.comments.push(commentProps);
          blogPost.save();
        })
    .catch((error) => res.status(400).json(error))
});

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

问题是你将整个评论对象推送到一个只应该有对象的数组中。

dnickless answer使用带引用的解决方案,这意味着您有一个blogposts集合和一个评论集合。博客文档将参考他们对objectids的评论。

您还可以更改博客帖子模型以使用嵌入而不是引用,这意味着评论将作为子文档的博客文档的一部分。关于什么更好herehere,有几个很好的讨论。简短的回答是它取决于用例。您可以自己选择想要使用的内容。

以下是嵌入操作的完成方式:

Blogpost模型:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const commentSchema = require('./comment.model');

const BlogPostSchema = new Schema({
  content: {
    type: String,
    validate: {
      validator: (content) => content.length > 5,
      message: 'Content must contain at least 6 characters.'
    },
    required: [true, 'Content must be filled in.']
  },
  rating: Number,
  title: String,
  user: { type: Schema.Types.ObjectId, ref: 'user' },
  board: {type: Schema.Types.ObjectId, ref: 'board'},
  comments: [commentSchema]
});

const BlogPost = mongoose.model('blogPost', BlogPostSchema);

module.exports = BlogPost;

注意comments数组如何使用注释模式而不是对象id数组。评论模型必须略有改变:

评论模型:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const CommentSchema = new Schema({
  content: {
    type: String,
    validate: {
      validator: (content) => content.length > 5,
      message: 'Content must contain at least 6 characters.'
    },
    required: [true, 'Content must be filled in.']
  },
  user: { type: Schema.Types.ObjectId, ref: 'user' },
  rating: Number

  // board: Board
});

// UserSchema.virtual('postCount').get(function(){
//   return this.posts.length;
// });

module.exports = CommentSchema;

const Comment = mongoose.model('comment', CommentSchema);已被删除。不应再注册架构,并且注释不会有自己的集合。现在正在导出模式而不是已注册的模型。

添加评论的原始代码应该在此之后起作用。评论将成为博客文档的一部分,而不是在他们自己的集合中。

答案 1 :(得分:0)

您不希望将整个Shape shape = ...; dump(shape); 推送到comment数组,而只推送到comments。所以像这样(未经测试):

_id