我正在学习 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);
}
}
最佳答案
updateProfile
使用 Modular SDK 时需要直接从 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")
}
}
关于javascript - updateProfile 不是函数 (Firebase),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70830141/