繁体   English   中英

updateProfile 不是 function (Firebase)

[英]updateProfile is not a function (Firebase)

我正在学习 React,但是当我注册用户(使用 createUserWithEmailAndPassword)并尝试更新 displayName 属性时,我收到错误消息“ user1.updateProfile is not a function ”。

我该如何解决这个错误?

我的代码:

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);
        }
    }

The updateProfile function needs to be imported from Firebase Auth SDK directly when using the Modular SDK as shown below:

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")
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM