firebase Firestore模拟器不适用于Expo Go

7rfyedvj  于 2023-03-24  发布在  Go
关注(0)|答案(1)|浏览(191)

我正在尝试将firestore模拟器与我正在使用Expo Go应用程序测试的应用程序一起使用。我已经将其设置为与常规react应用程序完全相同(其按预期工作)。然而,使用Expo Go应用程序测试应用程序一直抛出此错误:

@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.

我已经看过几个git问题和其他stackoverflow问题,但公认的答案似乎不起作用。This一个给了一些灵感,什么可能出错。和this github问题,其中一个人通过更新他的logrologram包修复了它。然而,更多的人似乎在9.6.* 中遇到了这个问题。
我的一个同事告诉我,它在iOS上的Expo Go应用程序上运行良好,我使用的是Android。此外,连接到实时数据库也可以工作。
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);

// Also tried with 
// const db = initializeFirestore(app, {
//  experimentalForceLongPolling: true,
// });
export const db = getFirestore();
// Also tried with my 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"

我真的抓住任何线索,因为它确实在我的同事的iOS手机上工作,但在我的Android上不工作。任何想法可能是导致这一点?

gcuhipw9

gcuhipw91#

此错误看起来像Firestore Emulator遇到的常见IPv6问题。
如果您在IPv6机器上运行,localhost将解析到本地IPv6地址,而Firestore Emulator正在侦听v4地址。现在的修复方法是使用显式IPv4地址环回:“127.0.0.1“
但是,如果这是一个物理Android设备,如果您在笔记本电脑或台式机上运行模拟器,并使用物理Android设备运行您的应用程序,IP地址不是localhost,而是连接的计算机的地址。在这种情况下,您应该能够按照以下说明连接到计算机的localhost
https://firebase.google.com/docs/emulator-suite/connect_firestore#java
10.0.2.2是根据上述内容尝试的IP地址。
另请参阅:Accessing localhost of PC from USB connected Android mobile device

相关问题