在本地调试 Firebase 函数 (node.js)

jam*_*phy 4 debugging firebase-realtime-database google-cloud-functions firebase-cli

看下面我的代码非常简单......它监听实时数据库中数据库字段的变化。

const functions = require("firebase-functions");
var admin = require("firebase-admin");

exports.onActiveUpdate = functions.database
  .ref("/profiles/users/{userId}/active")
  .onUpdate((change, context) => {
      //code here
      
    return true;
  });
Run Code Online (Sandbox Code Playgroud)

我尝试使用以下命令在本地调试代码

firebase serve --only functions
firebase serve
firebase emulators:start
Run Code Online (Sandbox Code Playgroud)

我总是收到同样的消息...

+  functions: Emulator started at http://localhost:5001
i  functions: Watching "C:\code\rn\xs\fb_functions\functions" for Cloud Functions...
i  functions[onActiveUpdate]: function ignored because the database emulator does not exist or is not running.
+  All emulators started, it is now safe to connect.
Run Code Online (Sandbox Code Playgroud)

但是当我转到 localhost:5001 时......我看到的只是一个{"status":"alive"}顶部带有 的空白页面。

这是正确的行为吗?谢谢

R..*_*... 9

您可以通过运行以下命令在检查模式下运行模拟器:

firebase emulators:start --inspect-functions
Run Code Online (Sandbox Code Playgroud)

查看它正在监听哪个端口进行调试(例如端口 9229 )。如果您使用 VSCode,请在项目的.vscode子目录中创建(或更新) launch.json ,其中包含以下内容:

   {
    "configurations": [
        {   "type": "node",
            "request": "attach",
            "name": "Debug",
            "port": 9229
        }
     ]
   }
Run Code Online (Sandbox Code Playgroud)

然后只需单击 VScode 中的“调试”图标即可连接到正在运行的进程。现在在函数代码中放置一些断点并通过浏览器调用它们。