Delete document using findOneAndRemove Mongoose Delete document using findOneAndRemove Mongoose mongoose mongoose

Delete document using findOneAndRemove Mongoose


The HTTP verb your using is not correctuse app.delete("/delete/:id", routes.delete_offer);

I think that should work. Cause I don't think there is no del method in the HTTP verb for express.js framework it mostly GET, POST, PUT, DELETE plus a few others.


If you using mongoose. You can fix file routes/index.js.

//Deleteexports.delete_offer = function (req,res){  Offer.findOneAndRemove({_id : new mongoose.mongo.ObjectID(req.params.id)}, function (err,offer){    res.redirect('/newsfeed');  });};


So you have a route set up for a DELETE verb in a RESTful sense. You don't seem to be calling it that way or using it in a RESTful way.

You application should really be handling this as a REST request, and issue status and content back as the response appropriate to what happened. Right now you are re-directing to another URL. That is not the correct approach. But If you just do not understand REST, then do it that way, but change your route to use GET instead.

For what it is worth, once you have sorted out your usage and testing, possibly using curl or similar as was shown. Then perhaps consider using .findByIdAndRemove() instead.

Offer.findByIdAndRemove(req.params.id, function (err,offer){    if(err) { throw err; }    // ...}

And then actually checking the response is what you expect before just forwarding or sending a valid or error response. Which is what you should be doing.