javascript - updateProfile 不是函数 (Firebase)

标签 javascript reactjs firebase 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);
        }
    }

最佳答案

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/

相关文章:

reactjs - 模块解析失败: es6 react component with JSX inside . JS文件

javascript - 如何在触发新更新时等待或取消 componentDidUpdate 中的数据更新?

ios - 在 Swift 中运行 Firebase 数据库快速入门时遇到问题

JavaScript - 改进在没有 Math.sqrt 的情况下查找完美平方根的算法

javascript - 需要相同的模块,不同的路径,变量不共享

javascript - 函数绘制混合属性

Firebase 身份验证 : How can I pre-create Auth Users for signin with the microsoft provider?

javascript - 为什么我的正则表达式捕获组在匹配多个部分时只捕获字符串的最后一部分?

javascript - 数组中数组的正确组件树在更改时重新渲染

androidx.room.RoomOpenHelper.e (RoomOpenHelper.java :15)