【发布时间】:2020-01-07 10:12:04
【问题描述】:
我有一个功能如下:
recieveUserData = async() =>{
console.log(firebase.auth().currentUser.uid);
let user =firebase.auth().currentUser.uid;
await firebase.database().ref('/Users/'+user+'/Public').once('value').catch(error=>{console.log("error:" + error)}).then((snapshot)=>{
//Do something.
})
}
控制台日志,实际上产生了 uid。但是,需要变量 'user' 并因此需要 'firebase.auth().currentUser.uid' 的函数失败:
[Unhandled promise rejection: TypeError: Cannot read property 'uid' of null].
我在想,也许 firebase.auth() 可能需要一些时间才能被调用,因此后面的行会出错。我想我可以通过将上面的内容更改为以下内容来解决这个问题:
console.log(firebase.auth().currentUser.uid);
await firebase.auth()
.catch(err=>{console.log(err)})
.then(response=>{console.log(response)});
我想我可以在 .then() 中写我后面的几行,但我得到一个不同的错误:
Unhandled promise rejection: TypeError: _firebase.default.auth(...).catch is not a function]
我很困惑,因为我在不同的模块中使用了类似的东西,而且它似乎工作正常。考虑到这些错误,我有点担心它现在会坏掉。任何见解都会非常方便。
提前致谢
【问题讨论】:
-
请不要在您的问题中包含答案代码。如果您对某个答案有评论,请将该评论留在那个答案下方。
-
太大了不能放在评论里哈哈。
标签: javascript firebase react-native firebase-realtime-database firebase-authentication