【问题标题】:firebase emulator making calls to production firestore instead of emulated firestorefirebase模拟器调用生产firestore而不是模拟firestore
【发布时间】:2021-10-01 07:32:44
【问题描述】:

我的函数模拟器正在调用生产版 Firestore,而不是 Firestore 模拟器。我不确定这是怎么发生的,因为我听说这是不可能的。

我已经设置了我的模拟器并加载了 Firestore 模拟器:

 Emulator       │ Host:Port      │ View in Emulator UI             │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Authentication │ 127.0.0.1:9099 │ http://127.0.0.1:4000/auth      │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Functions      │ 127.0.0.1:5001 │ http://127.0.0.1:4000/functions │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Firestore      │ 127.0.0.1:8080 │ http://127.0.0.1:4000/firestore │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Hosting        │ 127.0.0.1:5000 │ n/a                             │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Storage        │ 127.0.0.1:9199 │ http://127.0.0.1:4000/storage   │

这是我在函数中配置应用程序和 Firestore 的方式:

import { initializeApp } from 'firebase/app';
import {
  getFirestore,
} from 'firebase/firestore';

let firebaseConfig = {
  databaseURL: 'http://localhost:8080?ns=nft-public',
  projectId: 'nft-public',
};

const app = initializeApp(firebaseConfig);

const db = getFirestore(app);

还有我在 firebase.json 中的模拟器选项

  "emulators": {
    "auth": {
      "host": "localhost",
      "port": "9099"
    },
    "firestore": {
      "host": "localhost",
      "port": "8080"
    },
    "functions": {
      "host": "localhost",
      "port": "5001"
    },
    "hosting": {
      "port": 5000
    },
    "storage": {
      "port": 9199
    },
    "ui": {
      "enabled": true
    }
  }

【问题讨论】:

    标签: firebase google-cloud-firestore


    【解决方案1】:

    我遇到这个问题的原因是因为我是一个笨蛋。

    更多的技术原因是我试图在我的云函数中初始化 firebase,就好像它是一个外部应用程序(它使用“firebase/app”包)一样。

    在云函数中初始化 firebase 的正确方法是使用“firebase-admin”。该软件包还包含 Firestore 和您需要的所有其他功能。

    【讨论】:

    • 我们都会犯愚蠢的错误,别着急,感谢以后用户的解释!
    • 哈哈,谢谢,你学​​习编码的时候不能把自己当回事,我每天都会犯愚蠢的错误
    猜你喜欢
    • 1970-01-01
    • 2019-10-03
    • 2020-03-14
    • 2020-06-17
    • 2020-10-21
    • 2021-08-25
    • 2020-09-05
    • 1970-01-01
    • 2020-12-18
    相关资源
    最近更新 更多