블로그 서버의 코멘트 배열에 코멘트를 저장하는 Mongoose와 Mongo를 사용하여 노드 서버에서 경로를 만들고 있습니다 (모델 코드 게시 예정). 나는 그것이 나에게 다음과 같은 오류를 제공 쿼리를 실행하려고 할 때 : Postman error
이 내 모델과 경로입니다Mongoose의 배열에서 객체를 푸시하는 방법 (오류)
블로그 게시물 모델
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;
은
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))
});
모든 도움을 주시면 감사하겠습니다.
https://docs.mongodb.com/ecosystem/use-cases/storing-comments/ 그것의 파이썬하지만 당신은 $ push를 사용하여 아이디어를 얻을 – Gntem