Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I have these functions:

getPermissions: function(){
        return currentUser.permissions;
       }, //It returns permissions which is an array of strings as well.I use it in resolve first, before loading the page and it works

hasRole: function(){
        console.log(currentUser);
      return currentUser.role; //role is an array of objects but gives undefined
       }

currentUser is an object that looks like this:

currentUser

currentUser.role gives undefined probably because promise has not been resolved yet. I have to redirect the user just after login on basis of value contained in currentUser.role. How do I ? first resolve and then redirect? I think it really should solve the problem.

EDIT: I have above mentioned functions in a file called auth.js. auth.js has login function as well that looks like this:

 login: function(user, callback) {
        var cb = callback || angular.noop;
        var deferred = $q.defer();

        $http.post('/auth/local', {
          email: user.email,
          password: user.password
        }).
        success(function(data) {
          $cookieStore.put('token', data.token);
          currentUser = User.get();
          deferred.resolve(data);
          return cb();
        }).
        error(function(err) {
          this.logout();
          deferred.reject(err);
          return cb(err);
        }.bind(this));

        return deferred.promise;
      }

I am calling login function from auth.js in login.controller.js.

  $scope.submitted = true;

      if(form.$valid) {
        Auth.login({
          email: $scope.user.email,
          password: $scope.user.password
        })
        .then( function() {
          // Logged in, redirect to home
        var role = Auth.hasRole();
        console.log("role");
        console.log(role);  //gives undefined
       if(role.priority >= 1){
          $location.path('/admincontrol');

        }else{
          $location.path('/');

        }

        })
        .catch( function(err) {
          $scope.errors.other = err.message;
        });
      }
    };
share|improve this question
    
Yes, It gives can not read property 0 of undefined. – Simran Kaur Aug 5 at 13:32
    
Could you add where and how you initialize the "currentUser" var ? – Okazari Aug 5 at 14:03
2  
Are you sure, that currentUser initialized before you're trying to get access to role. Chrome lists object only after you're expanding it. Please provide part of code, which should initilalize currentUser – Andrey Aug 5 at 14:39
    
@Andrey: Could you please check the update ? – Simran Kaur Aug 5 at 21:18
    
@SimranKaur: And what User.get() returns? I can only guess that this is a promise. – Andrey Aug 6 at 12:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.