简体   繁体   English

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

[英]Firebase and React Auth how to update user information

I am trying to run two async functions however I keep getting the following error.我正在尝试运行两个异步函数,但是我不断收到以下错误。

error: Unhandled Promise Rejection: RangeError: Maximum call stack size exceeded.错误:未处理的承诺拒绝:RangeError:超出最大调用堆栈大小。

Can someone help with this.有人可以帮忙吗。 I need to update display name, email and password and I need them in different functions in my useAuth.js.我需要更新显示名称、电子邮件和密码,并且在我的 useAuth.js 中的不同功能中需要它们。 I only get the error when trying to run more than 1 function我只在尝试运行超过 1 个函数时收到错误

profile.js 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使用Auth.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);
}

These two method calls are invoking themselves ad infinitum:这两个方法调用无限地调用自己:

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

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

Note that your custom updateEmail invoked itself, as does updatePassword .请注意,您的自定义updateEmail调用, updatePassword也是如此。 You're probably trying to call the Firebase equivalents in there, but your custom functions are shadowing the Firebase imports.您可能正在尝试在那里调用 Firebase 等效项,但您的自定义函数正在影响 Firebase 导入。

You can rename your custom functions to not clash with the Firebase names:您可以重命名自定义函数,以免与 Firebase 名称发生冲突:

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

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

And then call this in handleSubmit :然后在handleSubmit中调用它:

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

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

相关问题 如何使用从 firebase 存储的 Auth uid 并更新 Auth 用户信息,例如更改密码、email 和电话号码? - How to use Auth uid that store from firebase and update Auth user information such as change password, email and phone number? 如何正确更新firebase中的用户信息? - How to properly update user information in firebase? 如何在 firebase.auth (js, ts) 中更新用户电话号码 - how to update user phone number in firebase.auth (js, ts) 如何在React Native中等待来自firebase auth的持久用户 - How to wait for persisted user from firebase auth in React Native React Native Firebase Auth 用户是 null - React Native Firebase Auth user is null 使用 React Router 将 Firebase Auth 用户发送到组件 - Send Firebase Auth user to component with React Router Firebase 云功能 1.0,当前用户没有身份验证信息 - Firebase cloud function 1.0, no auth information for current user 如何在反应原生中使用 firebase 检测 google auth 登录用户是否为 NewUser - How to detect whether google auth login user is a NewUser or not using firebase in react native 如何在反应中使用 firebase auth object? - How to use firebase auth object in react? 与 Firebase Auth 反应:如何设置持久性? - React with Firebase Auth: How to set Persistence?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM