[英]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.