Expo 不适用于 firebase 模拟器

Expo does not work with firebase emulator

我正在尝试通过我的 Expo 应用程序在物理设备和模拟设备上访问 firestore 模拟器(本地托管),但两者都不起作用。当我调用模拟的 firestore 数据库时,例如设置文档,它没有显示在 UI 上。但是,如果我 运行 与真正的 firestore 完全相同的代码,它可以正常工作。

我的 firebase.json 配置是:

{
  "firestore": {
    "rules": "firestore.rules",
    "indexes": "firestore.indexes.json"
  },
  "emulators": {
    "firestore": {
      "port": 8080
    },
  }
}

对于 android 模拟器上的 Expo 应用程序,我将配置设置为:

firebase.firestore().settings({
  host: "10.0.2.2:8080", 
  ssl: false,
})

对于我的物理 android 设备上的 Expo 应用程序:

firebase.firestore().settings({
  host: "localhost:8080",  
//I've also tried "192.168.68.109:8080" my computer's IP
  ssl: false,
})

原来你需要在firebase.json上指定主机为

"emulators": {
    "firestore": {
      "port": 8080
      "host": "0.0.0.0"
    },

至于物理设备上 Expo 应用程序的配置,而不是“localhost:8080”,将您的计算机(或您网络中的任何主机)IP 地址后跟“:8080”。

如果所有这些仍然不起作用,请在您的项目目录中尝试命令“firebase use ”,它应该会起作用。

如果您在物理设备上进行测试,使用计算机的 IP 地址进行连接会更方便。您可以使用内置变量,而不是对其进行硬编码:Constants.manifest.debuggerHost?.split(":").shift().

在这里写了一个简短的解释:

https://dev.to/haydenbleasel/using-the-firebase-local-emulator-with-expo-s-managed-workflow-5g5k