Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/458.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么我的代码不能访问刚刚创建的用户?_Javascript_Firebase_Firebase Authentication - Fatal编程技术网

Javascript 为什么我的代码不能访问刚刚创建的用户?

Javascript 为什么我的代码不能访问刚刚创建的用户?,javascript,firebase,firebase-authentication,Javascript,Firebase,Firebase Authentication,我使用以下代码创建用户: firebase.auth().createUserWithEmailAndPassword($scope.data.mail, $scope.data.pwd) .catch(function(error) { }); var user; while (!user) { user = firebase.auth().currentUs

我使用以下代码创建用户:

firebase.auth().createUserWithEmailAndPassword($scope.data.mail, $scope.data.pwd)
                .catch(function(error)
                       {
                       });

var user;

while (!user)
{
    user = firebase.auth().currentUser;
}

但是我不知道为什么这次用户变量总是得到一个空值:循环永远不会结束。我目前无法解决此问题。

您不能使用
while
循环等待异步结果,因为JavaScript在单个线程上运行

循环将无限期地执行,这意味着JavaScript运行时永远不会有机会处理承诺,即使它已经完成

您需要使用承诺中的
then
子句

firebase.auth().createUserWithEmailAndPassword($scope.data.mail, $scope.data.pwd)
  .then(function() {
     var user = firebase.auth().currentUser;
     // carry on executing code here
  })
  .catch(function(error) {
  });