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"
user2175731
  • 71
  • 1
  • 1
  • 2
  • 4
    Is not the error message enough? Your route requires a **DELETE** verb. You are trying to use **GET**. See [**here**](http://stackoverflow.com/questions/13371284/curl-command-line-url-parameters) for a start. – Neil Lunn Mar 24 '14 at 05:00
  • 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. – Parikshit Hooda Apr 24 '18 at 19:21

4 Answers4

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.

John Waweru
  • 51
  • 1
  • 5
  • 1
    `Cannot GET /delete/532fa5e56f885c7fec5223b1fds` You'll also have to use DELETE instead of GET wherever you're calling it from. – ki9 Dec 18 '17 at 00:38
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');
  });
};
Duy Nguyen
  • 161
  • 1
  • 3
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.

Community
  • 1
  • 1
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
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!'});
    });
}
Gaurav Gandhi
  • 3,041
  • 2
  • 27
  • 40
ie2020
  • 25
  • 7