1
mongoDB가 있는데 데이터베이스의 데이터를 조작하기 위해 Nodejs 서버를 만들려고합니다. 코멘트를 BlogPost 객체의 Comments 배열에 푸시하려고하면 castError가 발생합니다.MongoDB 및 Nodejs의 배열에 객체를 푸시하려고 할 때 왜 castError가 발생합니까?
아래의 소스 코드는 중요한 정보가 누락되었습니다. 미리 감사드립니다.
경로 :
routes.post('/comments/push/:id', function(req, res) {
const blogPostId = req.param('id');
const commentProps = req.body;
BlogPost.findById(blogPostId)
.then((blogPost) => {
blogPost.comments.push(commentProps);
return blogPost.save();
})
.then((blogPost) => res.status(200).json({
'status': 'Comment is deleted.',
'comment': blogPost
}))
.catch((error) => res.status(400).json(error)) });
블로그 게시물 스키마 :
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,
user: { type: Schema.Types.ObjectId, ref: 'user' },
board: {type: Schema.Types.ObjectId, ref: 'board'},
comments: [{
type: Schema.Types.ObjectId,
ref: 'comment'
}]
});
주석 스키마 : postman screen
,369 : 여기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
});
은 우체부에 오류가
도움을 주시면 대단히 감사하겠습니다!