Firebase 和 React Auth 如何更新用户信息

Firebase and React Auth how to update user information

我正在尝试 运行 两个异步函数,但是我不断收到以下错误。

error: Unhandled Promise Rejection: RangeError: Maximum call stack size exceeded.

有人可以帮忙吗?我需要更新显示名称、电子邮件和密码,并且我在 useAuth.js 的不同功能中需要它们。我只在尝试 运行 超过 1 个函数时出现错误

profile.js

//context 
const {
    updateDisplayName,
    updateEmail,
    updatePassword,
  } = useAuth();

//   when update button is pressed this function runs
const handleSubmit = async (event) => {
    // prevent page from reloading
    event.preventDefault();

    // make sure there no old error before making new request
    setErrorUpdating([]);

    await updateDisplayName(displayName)
    await updateEmail(email);
    await updatePassword(password)
}

useAuth.js

async function updateDisplayName(displayName) {
    const updateDisplayName = await updateProfile(currentUser, { displayName });
    // update the ui with the updated profile
    const newUserObj = { ...currentUser, displayName };
    setCurrentUser(newUserObj);

    return updateDisplayName;
}

async function updateEmail(email) {
    return await updateEmail(currentUser, email);
}

async function updatePassword(password) {
    return await updatePassword(currentUser, password);
}

这两个方法调用会无限地调用它们自己:

async function updateEmail(email) {
    return await updateEmail(currentUser, email);
}

async function updatePassword(password) {
    return await updatePassword(currentUser, password);
}

请注意,您的自定义 updateEmail 会自行调用,updatePassword 也是如此。您可能正在尝试调用其中的 Firebase 等效项,但您的自定义函数正在隐藏 Firebase 导入。

您可以重命名您的自定义函数,以免与 Firebase 名称冲突:

async function updateEmailForCurrentUser(email) {
    return await updateEmail(currentUser, email);
}

async function updatePasswordForCurrentUser(password) {
    return await updatePassword(currentUser, password);
}

然后在handleSubmit中这样调用:

const handleSubmit = async (event) => {
    ...
    await updateEmailForCurrentUser(email); // 
    await updatePasswordForCurrentUser(password); // 
}