【问题标题】:Firebase.default.auth().currentUser.uid is null after createUserWithEmailAndPassword()Firebase.default.auth().currentUser.uid 在 createUserWithEmailAndPassword() 之后为空
【发布时间】:2020-07-27 20:03:57
【问题描述】:

我正在尝试为在我的 React-Native 应用程序的注册屏幕上创建的任何新用户在 Firebase 上的“用户”集合中创建一个新文档,并且该文档应该包含新用户的 uid、名字、姓氏、电话号码和出生日期。问题是,在我使用 createUserWithEmailAndPassword 创建用户后,当我尝试使用 currentUser.uid 获取 uid 时,出现以下错误:null is not an object (evalating '_Firebase.default.auth().currentUser.uid ')。

我一直在尝试在 createUserWithEmailAndPassword 之后的 .then 语句中获取新用户的“uid”的方法,并在 .then 语句中创建新文档,但我还没有得到任何运气。我应该如何修改我的代码,以便在成功创建用户后能够成功创建新的“用户”文档?

下面是我的 handleSignUp 函数中的代码,当我的“注册”按钮被点击时被调用:

handleSignUp = () => {
    Firebase.auth()
      .createUserWithEmailAndPassword(this.state.email, this.state.password)
      .then(() => this.props.navigation.navigate("Main"))
      .catch((error) => this.setState({ errorMessage: error.message }));

    if (Firebase.auth().currentUser.uid) {
      const user = {
        uid: Firebase.auth().currentUser.uid,
        firstName: this.state.firstName,
        lastName: this.state.lastName,
        phone: this.state.phone,
        email: this.state.email,
        dob: this.state.dob
      };
      db.collection("users").doc(response.user.uid).set(user);
    }
  }; 

【问题讨论】:

    标签: firebase react-native google-cloud-firestore firebase-authentication


    【解决方案1】:

    如果你想:

    1. 创建用户
    2. 将他们的详细信息写入数据库
    3. 导航到新页面

    您需要确保按顺序执行这些步骤,并使用每个步骤返回的承诺来确保事情在正确的时间发生:

    Firebase.auth()
      .createUserWithEmailAndPassword(this.state.email, this.state.password)
      .then((credentials) => {
          const user = {
            uid: credentials.user.uid,
            firstName: this.state.firstName,
            lastName: this.state.lastName,
            phone: this.state.phone,
            email: this.state.email,
            dob: this.state.dob
          };
          return db.collection("users").doc(response.user.uid).set(user);
      }).then(() => {
          this.props.navigation.navigate("Main")
      }).catch((error) => this.setState({ errorMessage: error.message }));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-17
      • 2019-01-29
      • 1970-01-01
      • 1970-01-01
      • 2019-05-18
      • 1970-01-01
      • 2013-10-17
      • 1970-01-01
      相关资源
      最近更新 更多