简体   繁体   中英

Troubles using promise with AngularJS

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 : 安慰

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.

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);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM