Jamie
Jamie

Reputation: 10886

Angular Promise not working

I try to get some important things like: companyid,employeeid etc. with every request that a user makes. So this has to be received before everything else is done.

After that the user receives information based on his companyid that he sets with every request (get/company/{companyid}).

The problem that I have is that the response for receiving the companyid takes to long and angular already tries to make a request to (get/company/{companyid}) obviously there is no companyid yet.

I've tried to fix this whit promise but it's not working.

Here I try to receive some important information about the user(that I do with every request) :

Service

(function () {
    angular.module('employeeApp')
        .service('authenticationservice', authenticationservice);

    function authenticationservice($http,$location,authenticationFactory,$q,GLOBALS,$cookies) {

        this.validateUser = function () {
            var vm = this;
            vm.deferred = $q.defer();

            data = {"api_token": api_token};

            return $http.post(GLOBALS.url+'show/employee/' + $cookies.get('employeeid'),data)
                .success(function(response)
                {
                    vm.deferred.resolve(response);
                })
                .error(function(err,response)
                {
                    vm.deferred.reject(err);
                });

            return vm.deferred.promise;
        }
    }
})();

Routes file (In my routes file I use the authenticationservice to set all important users variables.)

employeeAppModule.run([
        'authenticationservice',
        'constants',
        function(authenticationservice,constants) {
            authenticationservice.validateUser()
                .then(function(response)
                {
                    constants.companyid = response.result.Employee;
                    constants.role = response.result.Role;
                    constants.name = response.result.FirstName;
                    console.log('test');
                },
                function(response){
                    console.log('error');
                });

        }
    ]);

So the problem is that the user information is set to late and angular already goes to my homeController where he uses the companyId that is not being set yet.

Thankyou

Upvotes: 0

Views: 2671

Answers (2)

Pankaj Parkar
Pankaj Parkar

Reputation: 136134

The problem in your current code is return $http.post are having two return statement in your validateUser method. Which is returning $http.get before returning return vm.deferred.promise; & that why customly created promise doesn't get returned from your method. Though by removing first return from $http.get will fix your problem, I'd not suggest to go for such fix, because it is considered as bad pattern to implement.

Rather I'd say, you should utilize promise return by $http method, & use .then to return data to chain promise mechanism.

Code

function authenticationservice($http, $location, authenticationFactory, $q, GLOBALS, $cookies) {
  this.validateUser = function() {
    var vm = this;

    data = {
      "api_token": api_token
    };

    return $http.post(GLOBALS.url + 'show/employee/' + $cookies.get('employeeid'), data)
      .then(function(response) {
      var data = response.data;
      retrun data;
    }, function(err) {
      return $q.reject(err);
    });
  }
}

Upvotes: 1

Mahmoud Shaaban
Mahmoud Shaaban

Reputation: 122

To make sure that $ http return a $ promise object you need to check that the action in the controller returns a value and it is not a void action.

Upvotes: 0

Related Questions