Reputation: 563
I'm trying to delete a record by using the _id, however i've getting a status 404 from postman. Where did things go wrong here?
router.delete('/sale/delete/:id', function(req, res) {
Sale.findByIdAndRemove({
_id: req.params.id
},
function(err, respRaw) {
if (err) {
console.log(err)
}
res.status(204).json(respRaw)
})
});
Upvotes: 2
Views: 346
Reputation: 1885
To add as a supplement for the author's answer:
findByIdAndRemove()
was confused with findOneAndRemove()
, namely its parameters.
Parameters
Returns:
Issue a mongodb findAndModify remove command by a document's _id field.
findByIdAndRemove(id, ...)
is equivalent tofindOneAndRemove({ _id: id }, ...)
.
Finds a matching document, removes it, passing the found document (if any) to the callback.
Executes the query if callback is passed.
Parameters
Returns:
Issue a mongodb findAndModify remove command.
Finds a matching document, removes it, passing the found document (if any) to the callback.
Executes the query if callback is passed.
Upvotes: 1
Reputation: 563
I managed to fix this by using
Sale.findByIdAndRemove(id, options, function(err, respRaw) {}
Upvotes: 0