我正在学习React,但当我注册用户(使用createUserWithEmailAndPassword(并尝试更新displayName属性时,我得到的错误为">user1.updateProfile不是一个函数"。
如何解决此错误?
我的代码:
const register = async (e) => {
if (name.length == 0) {
alert("name cannot be empty");
} else {
const userWithEmailAndPassword = await createUserWithEmailAndPassword(auth, email, password)
.then((auth1) => {
const user1 = auth.currentUser;
user1.updateProfile({
displayName: name
});
})
.catch(error => alert(error.message))
console.log(userWithEmailAndPassword);
}
}
使用Modular SDK时,updateProfile
函数需要直接从Firebase Auth SDK导入,如下所示:
import { createUserWithEmailAndPassword, updateProfile } from "firebase/auth"
const register = async (e) => {
if (name.length == 0) {
alert("name cannot be empty");
} else {
const { user } = await createUserWithEmailAndPassword(auth, email, password)
console.log(`User ${user.uid} created`)
await updateProfile(user, {
displayName: name
});
console.log("User profile updated")
}
}