【问题标题】:Cannot run firebase functions on localhost using firebase emulator:start无法使用 firebase 模拟器在 localhost 上运行 firebase 函数:启动
【发布时间】:2019-11-10 21:54:42
【问题描述】:

我尝试使用 firebase emulator:start 在本地计算机上测试我的 google firebase 云功能。然而,云函数的本地实例并未被执行,而是 Google Cloud 上的实例被执行。

我按照以下步骤设置模拟器:https://firebase.google.com/docs/functions/local-emulator

$ export GOOGLE_APPLICATION_CREDENTIALS='path/tofile'
$ firebase setup:emulators:firestore
$ firebase init firestore
$ export FIRESTORE_EMULATOR_HOST=localhost:8080
$ firebase emulators:start
 Starting emulators: ["functions","firestore","hosting"]
⚠  Your requested "node" version "8" doesn't match your global version "10"
✔  functions: Emulator started at http://localhost:5001
i  firestore: Logging to firestore-debug.log
✔  firestore: Emulator started at http://localhost:8080
i  firestore: For testing set FIRESTORE_EMULATOR_HOST=localhost:8080
✔  hosting: Emulator started at http://localhost:5000
i  hosting: Serving hosting files from: www
✔  hosting: Local server: http://localhost:5000
i  functions: Watching "/home/borch/Documents/Ionic/myproject/functions" for Cloud Functions...
i  functions: Setting up Cloud Firestore trigger "myGCFunction"
✔  functions: Trigger "myGCFunction" has been acknowledged by the Cloud Firestore emulator.

如您所见,触发器已被 Cloud Firestore 模拟器识别。

但每次调用 myGCFunction 时都会执行的函数是 Google Cloud 上的函数。我认为它应该从我在 localhost:8080 运行的本地 API 执行函数,对吗?

我意识到只有云上的函数会被执行,因为我检查了 firebase 函数控制台并查看了该函数执行的新记录。

我将 firebase-admin 和 firebase-functions 更新到了最新版本:

$ cat functions/package.json | grep firebase-
    "firebase-admin": "^8.2.0",
    "firebase-functions": "^3.0.2",

谢谢。

【问题讨论】:

  • 你是如何触发myGCFunction函数的?
  • 我正在使用模拟器提供的本地主机,网址为 localhost:5000 当 Firestore 上有新文档时触发 myGCFunction。

标签: firebase google-cloud-functions firebase-admin firebase-cli firebase-tools


【解决方案1】:

本地模拟器不会响应云中的变化。您必须实际更改本地模拟的数据库才能触发本地模拟的功能。

(想象一下,如果您不小心启动了一个本地模拟器来响应对繁忙的生产数据库的更改 - 这将无法在您的本地机器上很好地扩展,是吗?本地环境必须与任何云托管环境完全隔离,以便工作正常。)

【讨论】:

  • 嗨@Doug,如果我让模拟器在后台运行并且如果我对函数 index.ts 文件进行更改,我会看到检测到更改:
  • functions/src/index.ts changed, reloading triggers [debug] [2019-06-29T16:30:23.377Z] Functions runtime initialized. [debug] [2019-06-29T16:30:23.378Z] Disabled runtime features: undefined [debug] [2019-06-29T16:30:23.513Z] Found google-gax at /home/borch/Documents/Ionic/aws-iot-mqtt-client/functions/node_modules/google-gax/build/src/index.js [debug] [2019-06-29T16:30:23.513Z] Outgoing network have been stubbed. [debug] [2019-06-29T16:30:23.544Z] Checked functions.config() [debug] [2019-06-29T16:30:23.546Z] firebase-admin has been stubbed
  • 你有更多关于如何完全隔离模拟环境的信息吗?我从谷歌找到了这个:firebase.google.com/docs/functions/local-emulator 但它只是指本地环境中的云功能。
猜你喜欢
  • 2018-05-21
  • 1970-01-01
  • 2022-10-14
  • 2020-01-28
  • 1970-01-01
  • 1970-01-01
  • 2020-02-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多