Firebase createUserWithEmailAndPassword报undefined错误,Android项目缺失对应模块
问题
调用handleSignUp函数时触发undefined错误,但Firebase控制台显示用户账号已成功创建,且通过console.log("user mail at signup", user);能正常输出用户信息。此前该功能运行正常,即使注释掉这条console.log语句后的所有代码,错误仍然会出现。
错误信息如下:
Error signing up: You attempted to use a firebase module that's not installed on your Android project by calling firebase.app(). Ensure you have: 1) imported the 'io.invertase.firebase.app.ReactNativeFirebaseAppPackage' module in your 'MainApplication.java' file. 2) Added the 'new ReactNativeFirebaseAppPackage()' line inside of the RN 'getPackages()' method list.
附handleSignUp函数代码:
const handleSignUp = async () => { try { if (macAdd.length !== 12) { Alert.alert(t("signupFailed"), t("shortMacAdd")); return; } const userCredential = await createUserWithEmailAndPassword( auth, email, password ); const user = userCredential.user; console.log("user mail at signup", user); await updateProfile(user, { displayName: macAdd }); const token = await messaging().getToken(); const dataRef = ref(database, macAdd + "/"); await update(dataRef, { FCM_token: token }); console.log("FCM_token", token); setIsDialog1Visible(true); } catch (err) { console.error("Error signing up:", err.code); if (err.code === "auth/email-already-in-use") { Alert.alert(t("signupFailed"), t("auth/email-already-in-useBody")); } else if (err.code === "auth/invalid-email") { Alert.alert(t("signupFailed"), t("auth/invalid-emailBody")); } else if (err.code === "auth/weak-password") { Alert.alert(t("signupFailed"), t("auth/weak-passwordBody")); } else if (err.code === "auth/missing-password") { Alert.alert(t("signupFailed"), t("auth/missing-passwordBody")); } else { Alert.alert(t("signupFailed"), t("otherErrorSignup")); } } };
解决方法
根据错误提示,问题出在Android项目中Firebase核心模块未正确配置,按以下步骤排查修复:
检查
MainApplication.java文件:- 确认已导入
io.invertase.firebase.app.ReactNativeFirebaseAppPackage - 确认在
getPackages()方法的返回列表中添加了new ReactNativeFirebaseAppPackage()
- 确认已导入
补充排查操作:
- 清理Android项目缓存:执行
cd android && ./gradlew clean命令,之后重新构建项目 - 确保所有React Native Firebase相关依赖的版本保持一致,避免版本不兼容导致的初始化问题
- 验证
auth实例是否正确初始化,确认Firebase核心应用在调用createUserWithEmailAndPassword前已完成初始化
- 清理Android项目缓存:执行
内容的提问来源于stack exchange,提问作者Thiwanka Hewavitharana
相关产品推荐
相关产品推荐

