0

MEAN stack newbie here. I'm having difficulty understanding how delete works in MEAN. I'm using this SO Q&A and tutorial as guides, but whenever I test it out I get an error saying the data can't be deleted. Can somebody tell me what I've been doing wrong?

Here are my codes:

Controller JS

$scope.deleteProduct = function (value, idx) {

    var this_id = value._id;

    // delete
    $http.delete('/api/products/delete:' + this_id)
        .success(function (data) {
            console.log(data);
        })
        .error(function (data) {
            console.log('Error: ' + data);
        })
}

Node Server

app.delete('/api/products/delete:', productController.delete);

Server's "Controller"

module.exports.delete = function (req, res) {
    Service.remove({
        _id: req.params._id
    }, function (err, service) {
        if (err) {
            res.send(err);
        }
        else {
            res.json({message: "Delete successful."});
        }
    });
}

This is how I understood this. Is this correct?

  1. Controller JS gets the id to be deleted and calls $http's delete request(?), using said ID and the /api/products/delete:.

  2. Node Server sees that I called '/api/products/delete:' and passes the request to Server's Controller to complete the request.

  3. Server's Controller deletes the data and returns status.

Where did I go wrong? Please help.

Also, I've been seeing some posts that say $resource works better than $http. Why?

Thank you.

Community
  • 1
  • 1
Dee J.
  • 365
  • 4
  • 21

1 Answers1

1

I think you've got a couple things wrong here.

In Express in order to use params you need to have something in the route that can be replaced. i.e /api/:id express replaces the :id with whatever you pass in so if you send /api/1, request.params.id is 1

So first problem is your route is

app.delete('/api/products/delete:', productController.delete);

tha dosen't mean anything to Express. I think you want

app.delete('/api/products/:id', productController.delete);

now req.params.id should contain the parameter you send. Note im dropping the underscore here. you could use

app.delete('/api/products/:_id', productController.delete); and keep the underscore if you like.

Second mistake I think is your Angular code. you have the : in your call it should just be

$http.delete('/api/products/' + this_id)

Now you're sending the route with whatever Id you are trying to delete i.e

/api/products/1

Now Express gets that and can map it to /api/products/:id and replace the id and now your controller should work. barring any other issues.

Edit

I'm not very familiar with Angular but I think the reason people are saying to use $resource is it is easier. You can directly call the different HTTP verbs directly on the objects themselves objects like product.update and product.delete rather than trying to craft the http calls yourself. I'm sure there is a lot more to it than that but its a feature that's built into Angular that can be leveraged. I think one of the catches is the URLs for the resources just have to be set up a specific way on the server but I believe there was a way to override them in Angular.

ThrowsException
  • 2,586
  • 20
  • 37
  • Thank you so much for the very detailed explanation! So that's what the `:` stands for. :) They never really explained it in any of the resources I found. I changed my code according to your tips and they're working perfectly now. Thank you! – Dee J. Jan 01 '17 at 03:28