在createUserWithEmailAndPassword()之后,Firebase.default.auth().currentUser.uid为null [英] Firebase.default.auth().currentUser.uid is null after createUserWithEmailAndPassword()

查看:60
本文介绍了在createUserWithEmailAndPassword()之后,Firebase.default.auth().currentUser.uid为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create a new document on my "users" collection on Firebase for any new user created on the Signup screen for my React-Native app, and the document is supposed to include the new user's uid, first name, last name, phone number, and date of birth. The issue is, after I use createUserWithEmailAndPassword to create a user, when I try to grab the uid with currentUser.uid, I get the following error: null is not an object (evaluating '_Firebase.default.auth().currentUser.uid').

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

I've been experimenting with ways to get the new user's "uid" in the .then statement following createUserWithEmailAndPassword and also creating the new document within the .then statement but I haven't gotten any luck with that yet. How should I modify my code so that I'm able to successfully create a new "users" document after successfully creating a user?

下面是我的handleSignUp函数中的代码,该代码在单击注册"按钮时被调用:

Below is my code from my handleSignUp function that is called when my "Sign Up" button is clicked:

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);
    }
  }; 

推荐答案

如果要:

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

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

You'll need to make sure you do these steps in that order, and use the promises returned by each step to make sure things happen at the right time:

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 }));

这篇关于在createUserWithEmailAndPassword()之后,Firebase.default.auth().currentUser.uid为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆