Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS ngResource delete event not calling callback

I have this code:

dogsResource.delete({id: $stateParams.dogId}, angular.noop,
        function(value, responseHeaders){
            //Success
            console.log(value);
            console.log(responseHeaders);
        },
        function(httpResponse){
            //Error
            console.log(httpResponse);
        }
    );

The delete is done, the problem is that neither success nor error is being called. I've also tried using an instance (that means, to use $delete), but it didnt work either.

I tried testing the callbacks with other methods, such as get

$scope.dog = dogsResource.get({id: $stateParams.dogId}, function(value, res){
    console.log(value);
});

And it works. I don't know why that happen, since the dog is being deleted from database.

Thanks

UPDATE

dogResource code

// Return the dogs resource
.factory('dogsResource', ['$resource', function($resource){
    return $resource("http://localhost:5000/dogs/:id",{id: "@id"},{update: {method: "PUT"}});
}])

UPDATE 2

I Found the error. It was in the RESTful API (Node js). The method was not sending anything to Angular, so no callback was triggered:

//DELETE - Delete a dog with specified ID
exports.deleteDog = function(req, res) {
    console.log('DELETE' + req.params.id);

    Dog.findById(req.params.id, function(err, dog) {
        dog.remove(function(err) {
            if(err) return res.status(500).send(err.message);
            console.log('Succesfully deleted.');
            res.status(200);
        })
    });
};

Replacing res.status(200) with res.status(200).end() got the callback triggered.

Thanks you all for your time.

like image 255
CamiloR Avatar asked Dec 30 '15 14:12

CamiloR


2 Answers

I suggest to you to not use

res.status(200).end()

In fact usually when you delete an object with a REST service in expressJS, the common case is to send the deleted object as response, because it could be useful for the frontend to get this object (and to make sure that it's the good object).

So instead of use

res.status(200).end()

use

res.send(dog)

Or if you want to send an empty response, the status code for a delete operation should be :

res.status(204).end()

204 NO CONTENT

Note that you don't need to set the status code by default it will be 200. So set status code to 200 is just useless.

And to finish an http response needs to be sent to close the request. The end method or the send method make that. Set a status code to a response http will never send anything to the frontend. That's why your angular callback was never fired.

So i suggest to you to add the tag expressjs to your question, because it's not an AngularJS problem but a real expressJS mistake.

like image 83
Thomas Pons Avatar answered Nov 07 '22 08:11

Thomas Pons


In your code, the second argument is angular.noop:

dogsResource.delete({id: $stateParams.dogId}, angular.noop,
        function(value, responseHeaders){
            //Success
            console.log(value);
            console.log(responseHeaders);
        },
        function(httpResponse){
            //Error
            console.log(httpResponse);
        }
    );

According to the ngResource Source Code, if you set the second argument to a function (angular.noop is a function) then it will use the second argument as the success callback. Since the second argument is a no-operation, nothing will happen when it is called.

Try setting the second argument to function (r) { console.log (r) } and see what you get.

like image 30
georgeawg Avatar answered Nov 07 '22 08:11

georgeawg