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
Firebase身份验证类型错误:无法读取属性';getIdToken';空的_Firebase_Ecmascript 6_Firebase Authentication - Fatal编程技术网

Firebase身份验证类型错误:无法读取属性';getIdToken';空的

Firebase身份验证类型错误:无法读取属性';getIdToken';空的,firebase,ecmascript-6,firebase-authentication,Firebase,Ecmascript 6,Firebase Authentication,我正在尝试使用获取用户令牌 import { auth, database } from "firebase"; await token = await auth().currentUser.getIdToken(); 但是相反,我得到了这个错误 TypeError: Cannot read property 'getIdToken' of null 它有时有效,但有时无效。我做错了什么?根据安全问题,我认为这不是最好的主意,但我发现它很有用: firebase.auth().onAu

我正在尝试使用获取用户令牌

import { auth, database } from "firebase";

await token = await auth().currentUser.getIdToken();
但是相反,我得到了这个错误

TypeError: Cannot read property 'getIdToken' of null

它有时有效,但有时无效。我做错了什么?

根据安全问题,我认为这不是最好的主意,但我发现它很有用:

  firebase.auth().onAuthStateChanged(function(user) {
         if (user) {
          user.getIdToken().then(function(data) {
           console.log(data)
          });
         }
        });
let currentUser;
auth.onAuthStateChanged(user => {
  if(user) {
    currentUser = user;
  } else {
  }
})

@如果这对我没有帮助,我将用户设置为null。如果您需要令牌并且身份验证状态没有更改,该怎么办?