在本地运行Firebase触发事件

时间:2020-05-01 18:59:29

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

我有一个简单的方法,例如

exports.updatePlayer = functions.firestore
  .document('matches/{docId}')
  .onCreate((change, context) => {
    console.log('fired------------', context.params.docId);
  });

如果我向匹配项添加新文档,则可以在日志中看到控制台消息。

问题是如何在本地运行它?我在package.json中有这个cmd

"serve": "firebase emulators:start --only functions,database",

当我运行`npm run server'时,控制台会显示

> functions@ serve D:\cric\fs-functions\functions
> firebase emulators:start --only functions,database

i  emulators: Starting emulators: functions
!  database: Not starting the database emulator, make sure you have run firebase init.
+  hub: emulator hub started at http://localhost:4400
!  Your requested "node" version "8" doesn't match your global version "12"
+  functions: functions emulator started at http://localhost:5001
i  functions: Watching "D:\cric\fs-functions\functions" for Cloud Functions...
!  functions: The Cloud Firestore emulator is not running, so calls to Firestore will affect production.
+  functions[allMatches]: http function initialized (http://localhost:5001/xxxx-xxxxx/us-central1/allMatches).
i  functions[updatePlayer]: function ignored because the firestore emulator does not exist or is not running.
+  emulators: All emulators started, it is now safe to connect.

但是现在,如果我向firestore集合中添加一些内容,则在本地终端上不会收到任何控制台消息。还不是那样 i functions[updatePlayer]: function ignored because the firestore emulator does not exist or is not running.

我想在本地运行此触发器,因此可以快速编写/测试我的代码。

1 个答案:

答案 0 :(得分:1)

尝试更新您的NodeJs版本

Firebase宣布弃用Cloud Functions上的Node.js 8运行时,并弃用Node.js 6运行时。

要确保您的功能在Node.js的受支持版本上,请尽快迁移到Node.js 10运行时。

通过setting the engines field in package.json显式指定从Node.js 8更新到Node.js 10,以指定Node.js 10运行时。

NodeJs End-of-Life Releases