我正在尝试向 mongo db 发出测试发布请求。在邮递员中,我收到此错误:
{
"success": false,
"message": "Post validation failed: photo: Path `photo` is required., body: Path `body` is required., snippet: Path `snippet` is required., title: Path `title` is required."
}
这是邮递员的截图。
const postSchema = new Schema({
title: {
type: String,
required: true
},
snippet: {
type: String,
required: true
},
body: {
type: String,
required: true
},
photo: {
type: String,
required: true
},
}, { timestamps: true });
const Post = mongoose.model('Post', postSchema);
module.exports = Post;
这是我的架构。
router.post("/add-story", upload.array('photo', 10), async(req, res) => {
try{
let post = new Post();
Post.title = req.body.title;
Post.description = req.body.description;
Post.photo = req.body.photo;
Post.snippet = req.body.snippet;
await post.save();
res.json({
status: true,
message: "Successfully saved."
});
} catch(err) {
res.status(500).json({
success: false,
message: err.message
});
}
});
这是 POST 请求。
我尝试关闭“required:true”,请求成功通过并在我的数据库中创建了一个空条目。