首页 > 解决方案 > 在本地运行 firebase 触发事件

问题描述

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

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.

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

标签: firebasegoogle-cloud-functionsfirebase-cli

解决方案


尝试更新您的 NodeJs 版本

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

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

从 Node.js 8 更新到 Node.js 10,通过enginespackage.json

NodeJs 报废版本


推荐阅读