Expo+Firebase开发遇createUserWithEmailAndPassword未定义错误(Hermes引擎)
问题解决:TypeError: undefined is not a function(createUserWithEmailAndPassword相关)
Hermes JS引擎和Firebase Web SDK不存在兼容性问题,你的错误是Firebase Auth实例的导入/导出方式错误导致的,以下是具体解决办法:
方案一:修正Firebase模块的导出方式
修改firebase.js的导出逻辑,直接导出auth实例,而非包装在对象中:
import { initializeApp } from "firebase/app"; import { getAuth } from "firebase/auth"; const firebaseConfig = { apiKey: "", authDomain: "", projectId: "", storageBucket: "", messagingSenderId: "", appId: "", measurementId: "", }; const app = initializeApp(firebaseConfig); const auth = getAuth(app); // 直接导出auth实例 export default auth;
这样SignUp.js里的原有导入和调用代码就可以正常工作了。
方案二:修正SignUp模块的导入方式
如果不想修改firebase.js的导出,就在SignUp.js中正确解构导入的对象:
// 解构获取auth实例 import { auth } from "../firebase/firebase"; function createUser(email, password) { // 确保email和password参数已传入 auth.createUserWithEmailAndPassword(email, password) .then(() => { console.log("worked"); }) .catch((error) => { // 添加错误捕获,方便排查问题 console.error("注册失败:", error.message); }); }
额外注意事项
- 确保
email和password变量已正确定义并传入createUser函数,避免出现未定义参数 - 建议保留错误捕获逻辑,便于处理邮箱格式错误、密码强度不足等注册失败场景
内容的提问来源于stack exchange,提问作者Palm
相关产品推荐
相关产品推荐

