【问题标题】:firebase.auth().currentUser.uid - can't find uid of undefinedfirebase.auth().currentUser.uid - 找不到未定义的 uid
【发布时间】: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


【解决方案1】:

您不需要同时使用同步和异步。

您不熟悉“async”和“await”的用法。如果你理解'sync',你可以看到你的代码有问题。您将其设为“async”函数并将“await”放入 firebase。然后函数从firebase开始,所以'user'的值没有定义,它生成null。这会导致错误。 '.then' 的 Promise 与 'async' 和 'await' 功能相同。

你能试试这个代码吗?

//async
const {currentUser} = await firebase.auth()
let userUid = currentUser.uid

//sync
let userUid;
firebase.auth().then( currentUser => {
   userUid = currentUser.uid
})
.catch(error => console.log(error) )


【讨论】:

  • 我根据您的回答将代码编辑为以下内容。代码有时有效。我不确定为什么会这样。 recieveUserData = async() =>{ const {currentUser} = await firebase.auth() let userUid = currentUser.uid; //console.log(userUid); await firebase.database().ref('/Users/'+userUid+'/Public').once('value').catch(error=>{console.log("error:" + error)}).then ((snapshot)=>{//做某事}));
  • 玩了一会后,我认为我的代码没有正确实现等待。它在没有等待的情况下运行下一行,因此如果它在 Firebase 返回之前到达下一行,我会收到一个错误,但如果 Firebase 更快,代码可以工作。是否无法将 await 与 firebase.auth() 一起使用有什么原因?
  • 你不熟悉'async'和'await'的使用。如果您了解“同步”,您可以看到您的代码存在问题。您将其设为“异步”功能并将“等待”放入火力基地。然后函数从firebase开始,所以'user'的值没有定义,它生成null。这会导致错误。 '.then' 的 Promise` 与 'async' 和 'await' 函数相同。
  • @HarryShotta 如果我的回答有帮助,请选择“V”符号和箭头作为答案吗?
  • 感谢您的回答。抱歉,我仍然不确定您的意思。我应该将我的功能更改为什么? recieveUserData = async() =>{ const {currentUser} = await firebase.auth() let userUid = currentUser.uid; //console.log(userUid); await firebase.database().ref('/Users/'+userUid+'/Public').once('value').catch(error=>{console.log("error:" + error)}).then ((snapshot)=>{//做某事}))
【解决方案2】:

最后,我设法测试了我是否在父模块的 ComponentDidMount 中进行了身份验证,并且仅在我通过身份验证时才呈现相关模块。我还使用了 firebase.auth().onAuthStateChanged,因为它会在我通过身份验证后立即更新,即它给了 firebase 足够的时间来返回实际的 currentUser 而不是 null。这意味着我的初始代码/我现在尝试过的所有其他不同解决方案都可以正常工作:

class WholeProject extends Component {


state={
    user:null
}

componentDidMount = () => {
    firebase.auth().onAuthStateChanged((user)=>{
        if (user) {
            this.setState({ user });
        } else {
            this.setState({ user: null });
        }

    });}


render(){
let authenticated;
if (this.state.user === null)
authenticated=<Text>Unauthenticated</Text>;
else authenticated=<Profile user = {this.state.user}/>;


return(
<React.Fragment>
{authenticated}
</React.Fragment>

);

}
}

export default WholeProject;

【讨论】:

    猜你喜欢
    • 2023-03-14
    • 2021-01-17
    • 1970-01-01
    • 2012-12-10
    • 1970-01-01
    • 2012-07-27
    • 1970-01-01
    • 2017-01-21
    • 1970-01-01
    相关资源
    最近更新 更多