admin管理员组文章数量:1310542
Code:
function deleteItem(req, res) {
Goods.findByIdAndRemove(req.params.id, (err) => {
if (err) {
res.send({
success: false,
error: err
});
} else {
res.send({
success: true,
item: req.params.id
});
}
})
}
If I pass an _id
of just deleted document - Mongoose successfully "deletes" it.
If I pass an _id
of never existed document, like 591dad9a1583ea0d1065d633
- it also "deletes" it.
Error throws only if pass trash like a34pnv530eargdzbs
.
Could somebody tell me, what's going on, please ? :)
Code:
function deleteItem(req, res) {
Goods.findByIdAndRemove(req.params.id, (err) => {
if (err) {
res.send({
success: false,
error: err
});
} else {
res.send({
success: true,
item: req.params.id
});
}
})
}
If I pass an _id
of just deleted document - Mongoose successfully "deletes" it.
If I pass an _id
of never existed document, like 591dad9a1583ea0d1065d633
- it also "deletes" it.
Error throws only if pass trash like a34pnv530eargdzbs
.
Could somebody tell me, what's going on, please ? :)
Share Improve this question asked May 18, 2017 at 15:14 SevaSeva 6852 gold badges7 silver badges18 bronze badges1 Answer
Reset to default 12If you check the related Mongoose documentation you will find the reason behind it:
Finds a matching document, removes it, passing the found document (if any) to the callback. http://mongoosejs./docs/api.html#model_Model.findByIdAndRemove
If the document doesn't exist in your database Mongoose wont throw an error. You should check the 2nd parameter of the callback:
Goods.findByIdAndRemove(req.params.id, function(err, doc) {
if(err || !doc) {
// Show an error page
}
});
本文标签: javascriptHow can Mongoose quotdeletequot nonexisting documentsStack Overflow
版权声明:本文标题:javascript - How can Mongoose "delete" non-existing documents? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741798662a2398088.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论