Javascript ref内的代码一次不执行angularjs
的this.verifyUserToken块中的代码不执行。我想这与异步调用有关,因为返回的数据还没有准备好,但我似乎不知道如何处理它Javascript ref内的代码一次不执行angularjs,javascript,angularjs,firebase,angularfire,firebase-authentication,Javascript,Angularjs,Firebase,Angularfire,Firebase Authentication,的this.verifyUserToken块中的代码不执行。我想这与异步调用有关,因为返回的数据还没有准备好,但我似乎不知道如何处理它 this.verifyUserToken = function(){ //Check if token matches existing token and if verified is true ref.orderByChild('token').equalTo(this.token).once('value')
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
我设置了一个按钮,这样你就可以看到它工作了
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");
}
})
};
由于
verifyUserToken
具有对firebase的关联调用,因此您可以处理该调用,并从此函数返回Promise
,只有在验证调用完成后才能继续使用您的registerUser
我设置了一个按钮,这样你就可以看到它工作了
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");
}
})
};
您需要将验证放入verifyUserToken的回调中。现在,它将继续验证,但将始终为空。请告诉我我的答案是否适用于您,或者您是否仍有任何疑问。:)您需要将验证放入verifyUserToken的回调中。现在,它将继续验证,但将始终为空。请告诉我我的答案是否适用于您,或者您是否仍有任何疑问。:)谢谢@adolfosrs,我已经改正了,但是我现在面临着一个完全不同的错误。未捕获(承诺中)TypeError:无法读取未定义的属性“token”。好心帮忙out@user1841445在您的代码中,您使用了
this.token
,我将其更改为token
进行测试,并尝试将其回滚到原始代码。您没有包括此信息来自何处,因此这将取决于您的实现。我已将令牌
替换为此.token
。错误来自这一行ref.orderByChild('token').equalTo(this.token)。一次('value')。然后(function(dataSnapshot){…
@user1841445您有用户令牌吗?我终于能够修复错误。我意识到定义的所有变量this.variable\u name
都返回了未捕获的(承诺中)TypeError:无法读取未定义的属性“variable\u name”。必须解决电子邮件
和密码
变量的相同问题。我在$scope
级别修复了声明变量的问题。但是,我想知道为什么这个。variable\u name
返回未定义的hanks@adolfosrs,不过我已经做了更正我现在面临着一个完全不同的错误TypeError:无法读取未定义的属性“token”。请提供帮助out@user1841445在您的代码中,您使用了this.token
,我改为token
进行测试。请尝试将其回滚到您的原始代码。您没有包括此信息的来源,因此这将取决于您的实现。我已经将token
与this.token
放在一起。错误来自此行ref.orderByChild('token').equalTo(this.token)。一次('value')。然后(函数(dataSnapshot){…
@user1841445您有用户令牌吗?我终于能够修复错误。我意识到所有定义的变量this.variable\u name
都返回了Uncaught(承诺中)TypeError:无法读取未定义的属性“variable\u name”。必须解决电子邮件
和密码
变量的相同问题。我已在$scope
级别修复了此问题。但是,我想知道此属性的原因。variable\u name
返回未定义的变量