ref 中的代码一旦不执行 angularjs
code inside ref once not executing angularjs
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
有一个对 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");
}
})
};
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
有一个对 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");
}
})
};