我正在學習 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);
}
}
uj5u.com熱心網友回復:
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")
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/420417.html
標籤:
上一篇:為什么背景顏色不先改變?
