【问题标题】:code inside ref once not executing angularjsref 中的代码一旦不执行 angularjs
【发布时间】:2016-07-04 14:25:40
【问题描述】:

this.verifyUserToken 块中的代码不执行。我想这与异步调用有关,因为返回的数据尚未准备好,但我似乎不知道如何去做。

this.verifyUserToken = function(){
            //Check if token matches existing token and if verified is true
            ref.orderByChild('token').equalTo(this.token).once('value').
            then(function(dataSnapshot){
                //If token matches
                    if(dataSnapshot.val()){
                        alert("Token Exists",dataSnapshot.val().token);
                        $scope.isVerified = "YES";
                    }else{
                        alert("Token does not exist",dataSnapshot.val());
                        $scope.isVerified = "NO";
                    }
            });
        }

this.registerUser = function(){
            console.log("Entered registerUser()");

            this.verifyUserToken();
            alert("The Value of isVerified:"+ $scope.isVerified);
            if($scope.isVerified == "YES"){
                    alert("Verifying User Token...",this.verifyUserToken());
                    $scope.auth.$createUser({
                    "email": this.email,
                    "password" : this.password
                }).then(function(userData){
                    alert("Successfully created user account with uid:", userData.uid);
                    //redirect to /userlogin if registration is successful
                    //this.changeVerifiedStatus();
                    alert("User verifed and changed");
                    $location.path('/userlogin');
                }).catch(function(error){
                    alert("Error Creating User:",error);
                });
            }else{
                alert("Token failed verification");
            }   
            };

【问题讨论】:

  • 您需要将验证放入您的 verifyUserToken 的 then 回调中。现在它将继续 isVerified 将始终为空
  • 让我知道我的答案是否适合您,或者您是否仍有任何疑问。 :)

标签: javascript angularjs firebase angularfire firebase-authentication


【解决方案1】:

由于verifyUserToken 有一个对firebase 的异步调用,您可以处理它从该函数返回一个Promise,并仅在验证调用完成后继续您的registerUser

我已经设置了一个jsFiddle,所以你可以看到它的工作。

this.verifyUserToken = function(){
  //Check if token matches existing token and if verified is true
  var verifyUserTokenPromise = new Promise(function(resolve, reject){                                               
    ref.orderByChild('token').equalTo(this.token).once('value').then(function(dataSnapshot){
      //If token matches
          if(dataSnapshot.val()){
              alert("Token Exists",dataSnapshot.val().token);
              $scope.isVerified = "YES";
          }else{
              alert("Token does not exist",dataSnapshot.val());
              $scope.isVerified = "NO";
          }
          //resolve the promise. you can pass any data here
          resolve($scope.isVerified);
    });
  });
  return verifyUserTokenPromise;
};

this.registerUser = function(){
  console.log("Entered registerUser()");

  this.verifyUserToken().then(function(result){
    alert("The Value of isVerified:"+ $scope.isVerified);
    if($scope.isVerified == "YES"){
            alert("Verifying User Token...",this.verifyUserToken());
            $scope.auth.$createUser({
            "email": this.email,
            "password" : this.password
        }).then(function(userData){
            alert("Successfully created user account with uid:", userData.uid);
            //redirect to /userlogin if registration is successful
            //this.changeVerifiedStatus();
            alert("User verifed and changed");
            $location.path('/userlogin');
        }).catch(function(error){
            alert("Error Creating User:",error);
        });
    }else{
        alert("Token failed verification");
    }   
  })
};

【讨论】:

  • 感谢@adolfosrs,我已经进行了更正,但是我现在面临一个完全不同的错误。 未捕获(承诺中)类型错误:无法读取未定义的属性“令牌”。请帮忙
  • @user1841445 在您的代码中,您使用的是this.token,而我更改为token 用于测试海豚尝试将其回滚为原始代码。您没有提供此信息的来源,因此这将取决于您的实施。
  • 我已经用this.token 替换了token。错误来自这一行ref.orderByChild('token').equalTo(this.token).once('value').then(function(dataSnapshot){...
  • @user1841445 你有用户令牌吗?
  • 我终于能够修复错误了。我意识到this.variable_name 定义的所有变量都返回了 Uncaught (in promise) TypeError: Cannot read property 'variable_name' of undefined. 必须解决 emailpassword 变量的相同问题。我修复了在$scope 级别声明它们的问题。但是,我想知道为什么 this.variable_name 返回 undefined
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-11
  • 2021-12-10
  • 1970-01-01
  • 1970-01-01
  • 2015-01-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多