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

React Native+Firebase关联手机号与密码时更新邮箱报错排查

React Native Firebase 更新邮箱报错解决

我在React Native中使用Firebase,用户账号已关联手机号与密码两种登录方式,尝试更新邮箱时控制台报错:Error updating email for password provider: [TypeError: undefined is not a function]。

我的代码

const handleSaveEmail = async () => {    
  try {      
    if (!currentUser) {        
      console.log("User is not available");        
      return;      
    }      
    console.log("Provider Data:", currentUser.providerData);      
    const newEmail = updatedEmail.trim();      
    console.log("Updated Email:", newEmail);  

    if (newEmail === "") {        
      console.log("Email cannot be empty");        
      return;      
    }  

    // 查找providerData中的"password"提供商
    const passwordProvider = currentUser.providerData.find(        
      (provider) => provider.providerId === "password"      
    );  

    if (passwordProvider) {        
      console.log("User is authenticated with email/password");        
      try {          
        // 更新password提供商关联的邮箱          
        await updateEmail(currentUser, newEmail);          
        // 更新本地状态          
        setUpdatedEmail(newEmail);          
        setIsEditingEmail(false);          
        console.log("Email update successful for password provider");        
      } catch (error) {          
        console.log("Error updating email for password provider:", error);        
      }      
    } else {        
      console.log("User is not authenticated with email/password");        
      console.log("You may need to handle this case as needed.");      
    }    
  } catch (error) {      
    console.log("Error updating email:", error);    
  }
};

控制台输出的用户提供商数据

Provider Data: [
  {"displayName": null, "email": null, "phoneNumber": "+11234567890", "photoURL": null, "providerId": "phone", "uid": "+11234567890"}, 
  {"displayName": "xyz", "email": "xyz@gmail.com", "phoneNumber": null, "photoURL": null, "providerId": "password", "uid": "xyz@gmail.com"}
]

报错信息

Error updating email for password provider:[TypeError: undefined is not a function]

问题原因及解决方法

报错核心是updateEmail调用方式错误。Firebase Auth的React Native SDK中,updateEmail是用户对象的实例方法,而非独立函数。

修正代码

将原调用代码:

await updateEmail(currentUser, newEmail);

改为:

await currentUser.updateEmail(newEmail);

额外注意事项

  1. 敏感信息变更(如邮箱)需要用户处于重新认证状态,若当前用户是通过手机号登录的,需先调用currentUser.reauthenticateWithCredential(),使用手机号或密码凭证完成重新认证后再执行邮箱更新。
  2. 确保currentUser始终是有效的Firebase用户对象,避免出现undefined导致的异常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 16:02:43