reactjs - React-Native - 无法将 displayName 和 photoURL 保存到 firebase
问题描述
在我的 react native 项目中注册期间,我发现很难将用户的 displayName 和 photoURL 保存到 firebase。
我使用文档(https://firebase.google.com/docs/reference/js/firebase.User.html#updateprofile)作为指南。
请参阅下面我的代码:
onRegister = () => {
firebase.auth().createUserWithEmailAndPassword( this.state.typedEmail, this.state.typedPassword)
.then((userCredentials) => {
if(userCredentials.user) {
console.warn(userCredentials.user)
userCredentials.user.updateProfile({
displayName: "Jane Q. User",
photoURL: "https://example.com/jane-q-user/profile.jpg"
})
.then(() => {
this.setState({ user: userCredentials.user })
console.warn("yo:", this.state.user)
})
}
}).catch((error) => {
console.warn(`error:, ${error}`)
})}
使用上述功能,我可以保存电子邮件和密码,但 displayName 和 photoURL 仍然返回 null。
请帮忙。
解决方案
您可以尝试重新加载然后获取当前用户
async function updateProfile() {
await firebase.auth().currentUser.updateProfile({
displayName: "Jane Q. User",
photoURL: "https://example.com/jane-q-user/profile.jpg"
});
await firebase.auth().currentUser.reload();
console.log(firebase.auth().currentUser.displayName); <-- check if it is updated
}
更新
onRegister = async () => {
try {
const userCredentials = await firebase
.auth()
.createUserWithEmailAndPassword(this.state.typedEmail, this.state.typedPassword);
if (userCredentials.user) {
console.warn(userCredentials.user);
await userCredentials.user.updateProfile({
displayName: 'Jane Q. User',
photoURL: 'https://example.com/jane-q-user/profile.jpg',
});
await userCredentials.user.reload();
this.setState({ user: firebase.auth().currentUser });
console.warn('yo:', this.state.user);
}
} catch (error) {
console.warn(`error:, ${error}`);
}
};
推荐阅读
- javascript - 控制台中的下一个js错误_next/data/QPTTgJmZl2jVsyHQ_IfQH/blog/post/21/.json
- javascript - 如何从“FOR”循环中添加我的输出并求和
- python - 如何在这里正确使用列表?
- javascript - 如何使用 onClick 从 JSX 组件向反应函数组件传递参数
- java - 项目命令无法识别项目 Spigot
- vb.net - 控制异步操作的数量
- google-cloud-platform - 当我有多个要转换的数据文件 (.txt) 时,如何使用 DataFusion 执行 ETL 操作?在 Datafusion UI 中使用函数
- java - Spring RetryTemplate 返回使用
- android - 为什么当用户导航到另一个片段时调用 ViewModel onCleared() 但在设备旋转后不调用?
- sockets - 我需要在客户端套接字应用程序上使用 Dart 的惯用解决方案