无法使用Firebase模拟器在本地主机上运行Firebase函数:

时间:2019-06-29 05:54:55

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

我尝试使用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上的函数。我认为它应该从在本地主机上运行的本地API执行功能:8080,对吗?

我意识到只执行云上的功能,因为我用该功能执行的新记录检查了Firebase功能控制台。

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

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

谢谢。

1 个答案:

答案 0 :(得分:1)

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

(想象一下,如果您不小心启动了一个本地仿真器,该仿真器对繁忙的生产数据库的更改做出了响应-无法在本地计算机上很好地扩展,对吗?本地环境必须与任何云托管环境完全隔离,以便明智地工作。)