0

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

3
  • $http already returns a promise which you could simply return. Why are you trying to wrap it in $q? Commented Mar 27, 2016 at 18:28
  • .success is deprecated. See docs docs.angularjs.org/api/ng/service/$http#deprecation-notice Commented Mar 27, 2016 at 18:29
  • You need to resolve the promise before returning it: vm.deferred.resolve(); Commented Mar 27, 2016 at 18:40

2 Answers 2

1

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);
    });
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.