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

3phpmpom  于 2023-01-05  发布在  Node.js
关注(0)|答案(5)|浏览(140)

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

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;
  });

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

firebase serve --only functions
firebase serve
firebase emulators:start

我总是得到同样的信息...

+  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.

但是当我转到localhost:5001时......我看到的只是一个空白页面,顶部有{"status":"alive"}
这是正确的行为吗?谢谢

rlcwz9us

rlcwz9us1#

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

firebase emulators:start --inspect-functions

查看它正在侦听哪个端口进行调试(例如端口9229)。如果您使用的是VSCode,请在.vscode子目录中包含launch.json

{
    "configurations": [
        {   "type": "node",
            "request": "attach",
            "name": "Debug",
            "port": 9229
        }
     ]
   }

然后简单地点击VSCode中的Debug图标来连接到正在运行的进程。现在在函数代码中放置一些断点,并通过浏览器调用它们。

bqujaahr

bqujaahr2#

您可以使用https://github.com/GoogleChromeLabs/ndb来调试httpfirebase函数。
在本地或全局安装它,并使用ndb运行普通的serve命令:
第一个月

gab6jxml

gab6jxml3#

我刚刚试用了firebase模拟器套件,发现它确实支持node.js Inspector,因此很好地支持分步本地调试。
假设您的所有模块都适合模拟器,那么我们可以集中精力启用本地调试。
我选择MS Code作为我的Inspector客户端。

  • 执行firebase emulators:start并验证您的函数在本地是否正常工作。
  • 转到MS代码运行/调试屏幕,选择“添加配置......”并选择“{}Node.js:附加到进程”以创建新的. vscode/launch.json。您将看到生成了名为“Attach by Process ID”的调试配置。
  • 从MS代码运行/调试屏幕,运行这个新的“通过进程ID附加”调试器。您将被要求选择一个节点进程来附加调试器。
  • 在所有其他node.js进程中,您应该看到2个“functionsEmulatorRuntime”进程(1个用于functionsEmulator本身,另1个用于您的函数代码)。尝试选择其中的每一个,并检查调试器是否能够在断点处暂停。
ou6hu8tu

ou6hu8tu4#

根据文档,启动云函数和实时数据库模拟器的命令行如下:

firebase emulators:start --only database,functions
x8goxv8g

x8goxv8g5#

pref给出的解决方案很有效,下面是我的. vscode/launch.json

{
    "configurations": [
        {   "type": "node",
            "request": "attach",
            "name": "Node.js-Debugger",
            "port": 9229
        }
    ]
}

下面是我的火力点命令。

$ firebase emulators:start --inspect-functions --import emulator_data --export-on-exit

相关问题