javascript - code inside ref once not executing angularjs -
code inside this.verifyusertoken block of not execute. guess has asynchronous calls being data returned not ready seem not know how go it.
this.verifyusertoken = function(){ //check if token matches existing token , if verified 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 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 uid:", userdata.uid); //redirect /userlogin if registration successful //this.changeverifiedstatus(); alert("user verifed , changed"); $location.path('/userlogin'); }).catch(function(error){ alert("error creating user:",error); }); }else{ alert("token failed verification"); } };
since verifyusertoken
has assync call firebase can handle returning promise
function , proceed registeruser
after having ther verify call finished.
i've set jsfiddle can see working.
this.verifyusertoken = function(){ //check if token matches existing token , if verified 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 not exist",datasnapshot.val()); $scope.isverified = "no"; } //resolve promise. can pass 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 uid:", userdata.uid); //redirect /userlogin if registration successful //this.changeverifiedstatus(); alert("user verifed , changed"); $location.path('/userlogin'); }).catch(function(error){ alert("error creating user:",error); }); }else{ alert("token failed verification"); } }) };
Comments
Post a Comment