I am making a route in a Node server using Mongoose and Mongo which stores a comment in the comments array in blogPost (I will post model code). When I try to execute the query it gives me the following error:
Postman error
These are my models and the route:
blogPost model
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;
comment model
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;
Route
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))
});
Any help is greatly appreciated.