【发布时间】: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