updateProfile 不是函数(Firebase)

JAY*_*IYA 5 javascript firebase reactjs firebase-authentication

我正在学习 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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

Dha*_*raj 15

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")
  }
}
Run Code Online (Sandbox Code Playgroud)