{React Native} Firebase - 在导航前更新用户值,只有在软刷新后才会生效。

问题描述 投票:2回答:1

我有这个简单的代码

 state = { email: '', password: '', userName: '', errorMessage: null }

handleSignUp = () => {     
  firebase.auth().createUserWithEmailAndPassword(this.state.email, this.state.password).then((userInfo) =>{ 
      userInfo.user.updateProfile({displayName: this.state.userName}).then((s) => {this.props.navigation.navigate('Navigator')})
  })
    .catch(error => this.setState({ errorMessage: error.message }))
}

在应用导航到导航仪屏幕之前,一切都能正常工作。在这里,当我登录我的 firebase.auth() 我得到这个对象。

  Object {
        "displayName": null,
        "email": "[email protected]",
        "phoneNumber": null,
        "photoURL": null,
        "providerId": "password",
        "uid": "[email protected]",
      },

请注意... displayName 是无效的。

问题是这样的... 只有在软刷新后我才会得到。

  Object {
    "displayName": "Bro",
    "email": "[email protected]",
    "phoneNumber": null,
    "photoURL": null,
    "providerId": "password",
    "uid": "[email protected]",
  },

请注意 displayName 现在有一个值。

我错过了什么?.then() 顺序不对吗,承诺顺序不对吗?

我很感激任何建议

reactjs firebase react-native firebase-authentication react-navigation
1个回答
1
投票

updateCurrentUser没有触发onAuthStateChanged。

参考资料

因此,我们需要在更新后重新加载强制更新当前用户。

firebase.auth().currentUser.reload()

firebase.auth().currentUser.getIdToken(true)

这样

state = { email: '', password: '', userName: '', errorMessage: null }

  handleSignUp = () => {     
    firebase.auth().createUserWithEmailAndPassword(this.state.email, this.state.password).then((userInfo) =>{ 
        userInfo.user.updateProfile({displayName: this.state.userName}).then(firebase.auth().currentUser.reload()).then((s) => {this.props.navigation.navigate('Navigator')})
    })
      .catch(error => this.setState({ errorMessage: error.message }))
  }

0
投票

也许你需要像这样改变你的方法。

handleSignUp = async () => {     
  const userInfo = await firebase.auth().createUserWithEmailAndPassword(this.state.email, this.state.password)
  await userInfo.user.updateProfile({displayName: this.state.userName})
  this.props.navigation.navigate('Navigator')
}
© www.soinside.com 2019 - 2024. All rights reserved.