【问题标题】:Firebase Emulators Requests to local FireStore unsuccessfulFirebase 模拟器对本地 FireStore 的请求不成功
【发布时间】:2020-12-18 14:58:05
【问题描述】:

Firebase 模拟器对本地 FireStore 的请求不成功

你好,

我正在尝试设置 Firebase 模拟器,但我的应用仍在连接到 Firebase(web)。当我执行 db.collection("users").add() 时,我看到 https://console.firebase.google.com 上的条目不在 http://localhost:4000/firestore 上(主机端口是 8080,查看器端口是 4000)

这是我到目前为止所做的:

  1. Firebase 模拟器:开始返回“所有模拟器都准备好了!”

  2. Firebase.json 看起来不错(端口看起来不错)

"emulators": {
    "functions": {
      "port": 5001
    },
    "firestore": {
      "port": 8080
    },
    "database": {
      "port": 9000
    },
    "hosting": {
      "port": 5000
    },
    "pubsub": {
      "port": 8085
    },
    "ui": {
      "enabled": true
}
  1. 在初始化应用程序之前,我将 databaseURL 更改为我的本地 firestore。 请注意,在视频“The Local Firebase Emulator UI in 15 minutes”(https://www.youtube.com/watch?v=pkgvFNPdiEs 5:19)中,David East 将整个配置对象替换为“config = { databaseURL: '...' }”,但它返回此错误: “未捕获的 FirebaseError:firebase.initializeApp 中未提供“projectId”,如果我添加 value-key projectId,它会返回丢失的其他错误。
if (location.hostname === "localhost") {
  config.databaseURL = "http://localhost:8080?ns=project_name";
}

firebase.initializeApp(config);
  1. Firebase 使用好项目 (Firebase Firestore works with real database but not emulator)
firebase use project_name
  1. 我还创建了 Google Cloud Platform 私钥...以防万一。

我认为我的问题可能出在第 3 步,但我找不到解决方法。你有什么想法吗?你会怎么做才能让它发挥作用?

谢谢!

【问题讨论】:

  • @DougStevenson 感谢您纠正拼写错误。
  • 我在观看同一个视频后遇到了完全相同的问题。

标签: javascript firebase firebase-cli


【解决方案1】:

您已经完成了大部分工作,但我认为连接到本地数据库模拟器的方法在制作视频后发生了变化。

我使用以下方法成功连接到本地数据库模拟器:

if (!firebase.apps.length) {
  let config = {
    apiKey: "-------------------",
    authDomain: "--------------",
    databaseURL: "------------------",
    projectId: "----------------",
    storageBucket: "---------------------",
    messagingSenderId: "--------------",
    appId: "----------------------",
    measurementId: "-----------------"
  };

  firebase.initializeApp(config);

  if (location.hostname === "localhost") {
    var db = firebase.firestore();
    db.settings({
      host: "localhost:8080",
      ssl: false
    });
  }
}

保留在线连接的原始配置,但如果 location.hostname === "localhost",则进行适当的更改。

firebase.initializeApp(config) 需要在检测应用本地运行的 if 语句之上。

好在app继续使用在线认证,数据库和函数连接都是本地的。

最新文档在这里:https://firebase.google.com/docs/emulator-suite/connect_and_prototype

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    • 2021-04-22
    • 1970-01-01
    • 2019-10-03
    • 2019-10-19
    • 1970-01-01
    • 2020-03-14
    相关资源
    最近更新 更多