React Native集成Firebase Authentication时出现auth不存在错误
问题解决:ReferenceError: Property 'auth' doesn't exist
核心原因
SignupScreen.js中直接调用了auth对象,但未从配置好的firebase.js文件中导入该变量,导致运行时无法找到这个对象。
修复步骤
1. 为SignupScreen.js添加auth导入
修改SignupScreen.js的导入区域,补充从firebase.js导入auth的语句(注意路径要和你的firebase.js实际位置匹配,若不在同一目录需调整路径):
import { View, Text, StyleSheet, Image, TouchableOpacity, TextInput, KeyboardAvoidingView } from 'react-native' import React, { useState } from 'react' // 新增auth导入语句 import { auth } from './firebase'
2. 确认Firebase Auth模块已安装
如果导入后仍有异常,检查是否安装了Firebase Auth模块,执行以下命令安装:
npm install @firebase/auth # 或使用yarn yarn add @firebase/auth
3. 可选:适配Firebase v9+模块化规范
若使用的是Firebase v9及以上版本,推荐改用模块化API写法(兼容性更好,也符合官方最新规范),修改firebase.js如下:
import { initializeApp } from "firebase/app"; import { getAuth } from "firebase/auth"; const firebaseConfig = { apiKey: "AIzaSyAbKgCge94KVTyv7SMyIx3kAu1BkJXBn5I", authDomain: "fir-auth-d2499.firebaseapp.com", projectId: "fir-auth-d2499", storageBucket: "fir-auth-d2499.appspot.com", messagingSenderId: "580829064333", appId: "1:580829064333:web:10f3dbd97070935f5806c4", measurementId: "G-0WLRSQLJKX" }; const app = initializeApp(firebaseConfig); const auth = getAuth(app); export { auth };
验证修复
重新启动React Native项目,点击注册按钮即可正常调用Firebase注册接口。
内容的提问来源于stack exchange,提问作者Muskan Verma
相关产品推荐
相关产品推荐

