0

I'm currently working on an application based on node.js, the server side seems to be running properly but I'm having hard times with the public side.

I'm trying to use a promise with AngularJS in a controller. Here's my code :

var userId;
var userEmail = "somethingICanRecognizeInCaseItDoesntChange";

function getUserInfos(){
    var deferred = $q.defer();
    if(SessionStorageService.get('token')){
        Auth.isAlive().success(function(user){
            $scope.user = user;
            console.log("user :", user);
            console.log("user email :", user.email);
            deferred.resolve(user.email);
        });
    }
    else {
        $scope.user = {};
        deferred.resolve([]);
    }
    return deferred.promise;
}

userEmail = getUserInfos();
console.log("result :", userEmail);

I thought that result in console would be the same as user email, but it's not. Here's the console log :

result : Object { $$state: Object }
user : Object { firstName: "super", lastName: "admin", email: "a", isSuperAdmin: true, _ts: "2016-05-03T11:17:06.943Z" } website-ranking.js:41:21
user email : a

Here's a screenshot of what I get by clicking on Object in my browser console : console

The problem is, whatever I try, I never have a as result. I tried things like getUserInfos().$$state, getUserInfos().$$state.value, getUserInfos().$$state.Object, getUserInfos().Object, getUserInfos().Object.value and none of these worked.

What do I miss here ? Am I using promise in a wrong way ? Am I forgetting something ?

Please don't hesitate to tell me if this question can be improved, I'll gladly add code, or any informations you may need to help me solve my problem.

Edit : the thing is, I already used most of the code elsewhere, it's only since I added promise that I'm a bit lost. Here for example, I use the Auth.isAlive() without any problem :

function getUserInfos(cb){
            if(SessionStorageService.get('token')){
                Auth.isAlive().success(function(user){
                    $scope.user = user;
                    cb();
                });
            }
            else {
                $scope.user = {};
                cb();
            }
        }

The $scope.user is modified and usable further in the application with no issue.

As the question has been tagged as possible duplicate, I'd like to explain a bite more : I know why I'm using a promise, and I think I understood the differents issues with async in javascript. Here my problem is that my code doesn't work and I can't understand why, though I already successfully used promises in the past. I'm not asking how to get a response from async call, but a little help understand where I went wrong while writting code. If you still think that's a duplicate, let me know in comments and I'll deal with it, it's possible that I missed something in the link provided.

6
  • I would suggest looking into the callback needed for your Auth.isAlive function. The object you are looking at doesn't look like a proper response and might mean the function returns multiple parameters Commented May 3, 2016 at 13:53
  • Joe Clay please check the last part of my edit, and tell me if you still think it's a duplicate or not. @Stephn_R : It'd be weird that Auth.isAlive function causes my problem since I used it without any problem before, please check my edit. Commented May 3, 2016 at 14:14
  • Your request for getting the user it's using $http or $resource? Please provide the code of the Auth.isAlive method. Commented May 3, 2016 at 14:29
  • 1
    @kazu: Hm, I suppose that question isn't quite the same as this one, although they're very similar. The basic problem is the same, though - you're calling an asynchronous function, and expecting it to immediately return its result. In this case, it's actually going to return a promise. You ought to be using it in the form: getUserInfos().then(function (email) { ... });. Commented May 3, 2016 at 14:30
  • @GonzaloPincheiraArancibia it's using $http. Commented May 3, 2016 at 14:30

1 Answer 1

4

Because your method getUserInfos() returns a promise, you're able to do this:

var userId;
var userEmail = "somethingICanRecognizeInCaseItDoesntChange";

function getUserInfos(){
    var deferred = $q.defer();
    if(SessionStorageService.get('token')){
        Auth.isAlive().success(function(user){
            $scope.user = user;
            console.log("user :", user);
            console.log("user email :", user.email);
            deferred.resolve(user.email);
        });
    }
    else {
        $scope.user = {};
        deferred.resolve([]);
    }
    return deferred.promise;
}

getUserInfos().then(function(_email){
  userEmail = _email;
  console.log("result :", userEmail);
});
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.