Expo Go搭配Firestore模拟器Android端无法连接问题求助
Android设备上Expo Go连接Firestore模拟器失败问题
在使用Expo Go测试App时,接入Firestore模拟器的配置与可正常运行的常规React项目完全一致,但Android设备上持续抛出以下错误:
@firebase/firestore: Firestore (9.17.2): Could not reach Cloud Firestore backend. Connection failed 1 times. Most recent error: FirebaseError: [code=unavailable]: The operation could not be completed This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.
已查阅多个社区问答,尝试过已采纳的解决方案但均无效。同事的iOS设备上Expo Go可正常连接模拟器,我的Android设备连接线上Firebase数据库则完全正常。
相关配置代码
firebase.ts
import { initializeApp } from "firebase/app"; import { getFirestore, connectFirestoreEmulator } from "firebase/firestore"; import {initializeFirestore} from 'firebase/firestore'; const firebaseConfig = { apiKey: "...", authDomain: "...", projectId: "...", storageBucket: "...", messagingSenderId: "...", appId: "...", measurementId: "...", }; const app = initializeApp(firebaseConfig); // 也曾尝试过以下初始化方式 // const db = initializeFirestore(app, { // experimentalForceLongPolling: true, // }); export const db = getFirestore(); // 也曾尝试替换成本机IP地址 connectFirestoreEmulator(db, "localhost", 8080);
firebase.json
{ "firestore": { "rules": "firestore.rules", "indexes": "firestore.indexes.json" }, "emulators": { "firestore": { "port": 8080, "host": "0.0.0.0" }, "ui": { "enabled": true }, "singleProjectMode": true } }
TestComponent.tsx
import { Text, TouchableOpacity } from "react-native"; import { addDoc, collection, getDoc, doc } from "firebase/firestore"; import { db } from "../../../firebaseConfig"; export const FirestoreTestComponent = () => { const handleFetchData = async () => { const newDoc = await addDoc(collection(db, "test"), { title: "This is test", }); console.log("Newdoc:", newDoc); }; return ( <> <TouchableOpacity style={{ backgroundColor: "lightblue", padding: 5, borderRadius: 10, marginTop: 20, }} onPress={handleFetchData} > <Text>Add data to firestore</Text> </TouchableOpacity> </> ); };
版本信息
"expo": "~47.0.12", "firebase": "^9.17.2"
请问可能的问题原因是什么?
内容的提问来源于stack exchange,提问作者Sylent
相关产品推荐
相关产品推荐

