3

Feel free to let me know if this isn't a common practice - I'm a fairly new programmer - but I thought I've seen APIs in the past that, when you submit a DELETE request to a resource (/todo/1234), some servers will return the object you just deleted in the response. Is that a thing? If so, I'd be interested in learning how to do it. Here's what I have:

.delete(function (req, res) {
    Todo.findById(req.params.todoId).remove(function (err) {
        if (err) res.status(500).send(err);
        res.send("Todo item successfully deleted");
    });
});

This code does delete the item, but I would like to return the item that got deleted in the response instead of a string message. If that's a normal/okay thing to do. If it isn't normal or okay for some reason, please let me know why and I'll just move on. Or perhaps there's a more common way.

This is what I found in the [RFC 7231 docs][1]:

If a DELETE method is successfully applied, the origin server SHOULD send a 202 (Accepted) status code if the action will likely succeed but has not yet been enacted, a 204 (No Content) status code if the action has been enacted and no further information is to be supplied, or a 200 (OK) status code if the action has been enacted and the response message includes a representation describing the status.

I'm having a hard time interpreting what the 200 response means - is it only kosher to send a string message (Success!) or an object containing a message attribute ({message: "Success!"})? Or can you do whatever you want there? What's the best practice in Express using Mongoose?

Thanks in advance for the help, and sorry for my noobness with HTTP stuff. [1]: https://www.rfc-editor.org/rfc/rfc7231#section-4.3.5

Community
  • 1
  • 1
bobbyz
  • 4,946
  • 3
  • 31
  • 42

1 Answers1

4

You should use findOneAndRemove! Something like:

Todo.findOneAndremove({ id: req.params.todoId }, function( error, doc, result) {
    // it will be already removed, but doc is what you need:
    if (err) res.status(500).send(err);

    res.send(doc.id);
});
Andrey Popov
  • 7,362
  • 4
  • 38
  • 58
  • I wasn't sure about using `findOneAndRemove()` or `findByIdAndRemove()` becaused of [this SO post](http://stackoverflow.com/questions/5809788/how-do-i-remove-documents-using-node-js-mongoose) and what a number of people are saying about those methods. See the second answer's comments. One person says "As noted in other comments to other answers, this bypasses middleware that is defined on the schema, and can be really dangerous. So only use it if you understand the impact that will have. For more info, see mongoosejs.com/docs/middleware.html". Is that something I need to worry about? – bobbyz Dec 03 '15 at 16:02
  • 1
    Usually only if you're using hooks on your schema to perform additional actions on delete. – Kevin B Dec 03 '15 at 16:06
  • However, this perfectly answers my question if I generally can be safe using them, assuming I'm not defining any pre/post hooks in my Schema. I'll play around with it and see how it works out. – bobbyz Dec 03 '15 at 16:06
  • 1
    Well everything depends on your logic. If you have middleware - then yes :) If you don't have any kind of hooks - no. You can simply rewrite it as a one query to `findById`, and then second (inside the first callback) to remove it. There is no "save" option - it depends on your code and logic. – Andrey Popov Dec 03 '15 at 16:07
  • Ah, so is it just as "dangerous" to do it the way I have it up there in the OP, where my `.remove()` isn't inside the callback but is chained onto the `.findById()` method? Does that also bypass any hooks on schema remove? I guess I'm still confused about the best way to accomplish a remove if I **do** have hooks that I want executed on delete... – bobbyz Dec 03 '15 at 16:16
  • Also, @AndreyPopov, I'm getting "undefined" when I `console.log(result)`. But the `doc` is working perfectly :) – bobbyz Dec 03 '15 at 16:22