reactjs - 如何在 Firebase 中注册用户后立即将 displayName 添加到用户?
问题描述
我对使用useRef
. 这是我的代码:
try {
await signup(emailRef.current.value, passwordRef.current.value);
var user = firebase.auth().currentUser;
user.updateProfile({
displayName: nameOfUserRef,
});
history.push("/");
} catch (error) {
console.log(error);
}
我的期望:我希望新添加的用户的 displayName 将更改为nameOfUserRef
发生了什么:它没有抛出错误,但是当我控制台 log 时user.displayName
,它显示null
.
解决方案
该updateProfile()
方法是异步的并返回一个 Promise,因此您应该使用await
,就像您对异步signup()
方法所做的那样:
try {
await signup(emailRef.current.value, passwordRef.current.value);
var user = firebase.auth().currentUser;
await user.updateProfile({
displayName: nameOfUserRef,
});
console.log(user.displayName) <= Should be ok here
history.push("/");
} catch (error) {
console.log(error);
}
推荐阅读
- c - 需要帮助解决 C 中的分段错误
- sql - 重新加入 SQL Server 中的同一张表
- prestashop - 产品特征长度的 Prestashop 问题
- laravel - Laravel 查询以列出结果
- regex - 使用正则表达式从 Tensorflow 2 中的 tf.Tensor 中提取字符串?
- javascript - 最好使用页面上的表格或预制的 javascript 变量中的数据 - PHP/Javascript
- python - 正则表达式 jupyter 笔记本
- python - 从 span 标签上方的文本节点获取文本
- javascript - 如何使用 Node.js 读取具有不同名称的文件?
- r - 如何将数据框的一行与向量值绘制为曲线?