首页 > 解决方案 > 我的 firebase 用户对象似乎不正确

问题描述

我是firebase的新手。

我正在尝试使用 firebase 身份验证制作注册组件,但我从 firebase.auth().createUserWithEmailAndPassword 返回的用户对象似乎不正确。

官方文档说我可以通过调用user.uid来获取uid,但在我的情况下它返回null。相反,我可以通过调用 user.user.uid 来获取 uid。我很混乱。

我看到的文件是不是已经过时了?还是我做错了什么?

详细代码如下。请看一看,给我一个建议。

const promise=firebase.auth.createUserWithEmailAndPassword(email, password);
promise
.then((user) => {
 console.log(user);
 console.log(user.id); //returns null
 console.log(user.user.uid); //returns O6hLcfJL5NT4fV2S7JprQ3MhjCK2, which I assumes to be uid.
})
.catch((e) => {
 console.log(e.message);
})

以下是 console.log(user) 的结果。

{user: P, credential: null, additionalUserInfo: Wf, operationType: "signIn"}
 additionalUserInfo: Wf {providerId: "password", isNewUser: true}
 credential: null
 operationType: "signIn"
 user: P {G: Array(0), l: "AIzaSyDIj8N9kONUkpOdMGuApNcKkaeFYYEzfyk", o: "[DEFAULT]", u: "bluegenie-2ba45.firebaseapp.com", c: bi, …}
 __proto__: Object

如您所见,我从 createUserWithEmailAndPassword() 方法返回的用户对象内还有另一个用户对象。

提前致谢。

标签: firebase-authentication

解决方案


createUserWithEmailAndPassword返回一个用 aUserCredential而不是 User 解析的 Promise。

此更改是在v5.0.0中进行的


推荐阅读