mark5
mark5

Reputation: 563

findByIdAndRemove getting status 404

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)
        })
});

enter image description here

Upvotes: 2

Views: 346

Answers (2)

Matin Sasan
Matin Sasan

Reputation: 1885

To add as a supplement for the author's answer:

findByIdAndRemove() was confused with findOneAndRemove(), namely its parameters.


findByIdAndRemove():

Parameters

  • id «Object|Number|String» value of _id to query by
  • [options] «Object» optional
  • [options.strict] «Boolean|String» overwrites the schema's strict mode option
  • [callback] «Function»

Returns:

  • «Query»

Issue a mongodb findAndModify remove command by a document's _id field.

findByIdAndRemove(id, ...) is equivalent to findOneAndRemove({ _id: id }, ...).

Finds a matching document, removes it, passing the found document (if any) to the callback.

Executes the query if callback is passed.


findOneAndRemove():

Parameters

  • conditions «Object»
  • [options] «Object» optional
  • [options.strict] «Boolean|String» overwrites the schema's strict mode option
  • [callback] «Function»

Returns:

  • «Query»

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

mark5
mark5

Reputation: 563

I managed to fix this by using

Sale.findByIdAndRemove(id, options, function(err, respRaw) {}

Upvotes: 0

Related Questions