我在向 MongoDB 发出发布请求时遇到问题。以下请求:
router.post("/updateReply/:id", (req, res) => {
let id = req.params.id;
CommentReply.findById({id}).then(comment => {
comment.description = req.body.description;
comment.save().then(reply => {
res.json(reply);
})
.catch(err => {
res.status(400).send(err);
});
})
.catch(err => {
res.status(200).send(err);
})
});
一直失败并出现以下错误:
{
"message": "Cast to ObjectId failed for value \"{ id: '5e137efeb2be3d4408ce374b' }\" at path \"_id\" for model \"commentReplies\"",
"name": "CastError",
"stringValue": "\"{ id: '5e137efeb2be3d4408ce374b' }\"",
"kind": "ObjectId",
"value": {
"id": "5e137efeb2be3d4408ce374b"
},
"path": "_id"
}
我不知道如何解决这个问题,尤其是因为我的 get 请求每次都成功运行:
router.get("/getReply/:id", (req, res) => {
let id = req.params.id;
CommentReply.findById(id).then(comment => {
res.json(comment);
});
});
最佳答案
不要向findById发送一个对象,发送一个字符串...
// not ({id})
CommentReply.findById(id)....
关于javascript - Mongoose POST 请求不会执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59617577/