Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete document using findOneAndRemove Mongoose

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"
like image 201
user2175731 Avatar asked Mar 24 '14 04:03

user2175731


People also ask

How do you delete an item from MongoDB using Mongoose?

There is currently no method called deleteById() in mongoose. However, there is the deleteOne() method with takes a parameter, filter , which indicates which document to delete. Simply pass the _id as the filter and the document will be deleted.

What is the Mongoose command used to delete an item?

Mongoose | deleteOne() Function The deleteOne() function is used to delete the first document that matches the conditions from the collection. It behaves like the remove() function but deletes at most one document regardless of the single option.

What is the difference between findOneAndDelete and findOneAndRemove?

Difference between findOneAndDelete() and findOneAndRemove() This function differs slightly from Model. findOneAndRemove() in that findOneAndRemove() becomes a MongoDB findAndModify() command, as opposed to a findOneAndDelete() command. For most mongoose use cases, this distinction is purely pedantic.


1 Answers

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.

like image 187
John Waweru Avatar answered Sep 23 '22 18:09

John Waweru