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

Why can my code not access user that was just created?

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

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) {
  });