1

My index.jade file(my button which the users clicks to delete the document) is:

a(href="/delete/#{booking.id}") Delete

So far in my index.js file is:

router.get('/delete/:id', function (req, res) {
  Booking.findOneAndDelete({ id: req.params.id }).remove().exec();
});

I also, tried:

router.get('/delete/:id', function (req, res) {
  Booking.findById(id, function (err, doc) {
  if (err) {
    message.type = 'Error!';
  }
  doc.remove(callback); //Removes the document
  });

Neither are working, I just get the url with the ID:

localhost:3000/delete/54d49430b08883dc2fc8bb0d

1 Answer 1

3

You need to execute before you can remove, you should also be performing a delete request and not a .get(). Try this:

router.delete('/delete/:id', function (req, res) {
    Booking.findById(req.params.id)
        .exec(function(err, doc) {
            if (err || !doc) {
                res.statusCode = 404;
                res.send({});
            } else {
                doc.remove(function(err) {
                    if (err) {
                        res.statusCode = 403;
                        res.send(err);
                    } else {
                        res.send({});
                    }
                });
            }
        });
});
Sign up to request clarification or add additional context in comments.

2 Comments

I tried this, I'm still getting the url change but it isn't delete document out of the database
Is that id the valid id? Can you look in your network tab and see what response it's giving? Also add some console.log()'s in this delete method to see if doc is there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.