You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firebase修改密码报错:无法读取undefined的email属性

问题解决:Firebase修改密码时无法读取email属性

报错根源

你遇到的Cannot read properties of undefined (reading 'email')错误,源于两个问题:

  • 拼写错误:Firebase Auth获取当前用户的正确属性是currentUser(首字母U大写),你写成了currentuser,导致user变量为undefined,自然无法读取email属性。
  • 缺少Promise返回:reauthenticate函数没有返回reauthenticateWithCredential的Promise,后续调用.then时会因调用undefined的方法报错。

修正后的代码

// 修正拼写错误,并返回Promise
reauthenticate = (currentPassword) => {
    var user = firebase.auth().currentUser; // 修正为currentUser
    var cred = firebase.auth.EmailAuthProvider.credential(user.email, currentPassword);
    return user.reauthenticateWithCredential(cred); // 返回Promise,供后续.then调用
}

reauthenticate(currentPassword).then(() => {
    var user = firebase.auth().currentUser; // 同步修正拼写
    user.updatePassword(newPassword).then(() => {
        alert("Password changed");
        window.location.replace("./index.html");
    }).catch((error) => {
        console.log(error);
    })
}).catch((error) => { // 新增重新验证环节的错误捕获
    console.log("重新验证失败:", error);
});

额外注意事项

  • 确保执行这段代码时用户处于已登录状态,firebase.auth().currentUser才会有有效值。如果用户未登录,需先处理登录逻辑。
  • 建议在获取currentUser后增加非空判断,避免意外报错:
    var user = firebase.auth().currentUser;
    if (!user) {
        alert("用户未登录");
        return;
    }
    

内容的提问来源于stack exchange,提问作者pmisrfvfpimm23

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 08:30:24