Delete document using findOneAndRemove Mongoose
Asked Answered
H

4

7

I am receiving this error when trying to delete a document from the database:

Cannot GET /delete/532fa5e56f885c7fec5223b1fds

How can I successfully delete the document?

app.js

//Delete 
app.del('/delete/:id', routes.delete_offer);

routes/index.js

    //Delete
    exports.delete_offer = function (req,res){
      Offer.findOneAndRemove({'_id' : req.params.id}, function (err,offer){
        res.redirect('/newsfeed');
      });
    };

views/dashboard.jade

        - each offer in offers
            div.offer.row
                a(href="/offer/" + offer._id)
                    div.columns
                        div.sell_type
                            p=offer.type
                    div.small-8.columns.sell_info
                        p.sell_price="$" + offer.fixedPrice() + " "
                        p.sell_location="@ " + offer.location + " ›"
                    div.small-4.columns.sell_pic
                        p=offer.user_id
                a.delete(href="/delete/" + offer._id)="Delete Offer"
Homothermal answered 24/3, 2014 at 4:56 Comment(2)
Is not the error message enough? Your route requires a DELETE verb. You are trying to use GET. See here for a start.Sulphone
make sure that you are actually posting to the route. if you are using browser to navigate to that route, it will actually make a get request. use postman to verify the api.Intrepid
H
5

The HTTP verb your using is not correct use 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.

Hachmin answered 18/6, 2015 at 5:46 Comment(1)
Cannot GET /delete/532fa5e56f885c7fec5223b1fds You'll also have to use DELETE instead of GET wherever you're calling it from.Concave
C
3

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

//Delete
exports.delete_offer = function (req,res){
  Offer.findOneAndRemove({_id : new mongoose.mongo.ObjectID(req.params.id)}, function (err,offer){
    res.redirect('/newsfeed');
  });
};
Creditor answered 2/7, 2016 at 7:2 Comment(0)
S
1

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.

Sulphone answered 24/3, 2014 at 5:18 Comment(0)
H
1

Notice if you use the Mongoose findByIdAndRemove function to retrieve and delete the object from the Model.

exports.delete_offer = function(req, res) {

    Offer.findByIdAndRemove(req.params.id, function(err) {
        if (err)
            res.send(err);
        else
            res.json({ message: 'Offer Deleted!'});
    });
}
Halfcaste answered 21/2, 2017 at 16:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.