1

I'm new to Angular and I'm using some of the features to post data to a endpoint - a endpoint that I have complete control over. Its a very simple endpoint with CRUD capability allowing me to work with "customer" entities for educational purposes.

I have a partial view that basically lists all the customer. On each row, there is a delete button that allows me to delete that customer. When deleting a customer a request is sent to the endpoint. The endpoint will then check if a customer is "admin", if so, return a JSON response which says "You cannot delete a admin", otherwise, delete the customer and send back "Customer deleted" JSON response.

Upon receiving a response from the server, the angular application should do logic depending on the response data. The endpoint is working as intended. However, I'm getting some strange/unexpected result.

Here is what my code looks like:

In a" SecretsController", I have the following:

   $scope.deleteUser = function (userName, $event) {
                    credentialsService.removeRegistredUser(userName).then(function (response) {
                        if (response.data.Feedback === "Error") {
                            $rootScope.currentStatus = "There was a error raised: " + response.data.Message;
                        } else {
                            $rootScope.currentStatus = "User sucessfully removed.";
                            var currentRow = $event.target.closest("tr");
                            $(currentRow).fadeOut(1500, function () {
                                currentRow.remove();
                            });
                        };
                        fireResponseToGui();
                    }, function (error) {
                        $rootScope.currentStatus = "Invalid request: " + error.status + ". The response is: " + error.statusText;
                        fireResponseToGui();
                    });
                };

And in :

             var deffered = $q.defer();

            var credentialsServiceFactory = {};

credentialsServiceFactory.removeRegistredUser = function (userMail) {
                $http.post(registratedUsersEndpoint + apiDelete + userMail + "&token=" + token)
                    .then(function (response) {
                        deffered.resolve(response);
                    }, function (error) {
                        deffered.reject(error);
                    });

                return deffered.promise;
            };

And there is the problem. After some checking with console.log(), the "return deffered.promise" line in the service seems to return the exact same result although the response object in the service is different. Anyone now what I'm doing wrong here?

JL-ConcepT
  • 111
  • 6
  • Classic [deferred anti-pattern](http://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it). Your problem is probably due to the declaration of `deferred` outside the `removeRegistredUser` function. Simply returning the `$http` promise should solve it – Phil Nov 08 '15 at 23:40
  • Thank you for your reply sir! I wasn't even aware of that I was doing that! You are correct; deferred was declared outside of removeRegistredUser(). Everything is working as intended when the variable "deffered" is moved in removeRegistredUser () And thank toy for the link! I'll – JL-ConcepT Nov 09 '15 at 00:08
  • yes you need to write var deffered = $q.defer(); in removeRegistredUser() function only then you will get correct response from service. – roshini Nov 09 '15 at 00:13

1 Answers1

1

To elaborate on my comment, the deferred object you return in removeRegistredUser is declared outside the function and thus will survive longer than the initial use. I'm not entirely sure but at a guess, I'd say you cannot resolve a promise more than once.

Simply return the promise created by the $http service

return {
    removeRegistredUser: function(userMail) {
        return $http.post(registratedUsersEndpoint + apiDelete + userMail + "&token=" + token);
    }
};
Phil
  • 157,677
  • 23
  • 242
  • 245