简体   繁体   English

调试 firebase 云函数

[英]Debugging firebase cloud functions

How do I debug with visual studio code firebase-database trigger functions?如何使用 Visual Studio 代码firebase-database触发函数进行调试? I tried the emulator, but I get an error when I call this我尝试了模拟器,但是当我调用它时出现错误

functions debug myMethod

C:\functions\functions>functions debug createUserChat
ERROR: Error: Function myMethod in location us-central1 in project myProject does not exist
at C:\Users\Dev\AppData\Roaming\npm\node_modules\@google-cloud\functions-emulator\node_modules\grpc\src\client.js:554:15

This code I want to debug这段代码我想调试

require('@google-cloud/debug-agent').start({ allowExpressions: true });;

const functions = require('firebase-functions'),
        admin = require('firebase-admin'),
        logging = require('@google-cloud/logging')();
        admin.initializeApp(functions.config().firebase);

exports.myMethod= functions.database.ref('Tasks/{taskID}/taskStatus').onUpdate(event =>{
       // do sth.
});

this is my launch file这是我的启动文件

{
"version": "0.2.0",
"configurations": [
    {
        "name": "Debug Function",
        "type": "node",
        "request": "attach",
        "port": 5858
    }
]
}

debug-agent required only for remote debugging. debug-agent仅用于远程调试。 If you want to debug function locally use Cloud Functions Emulator.如果您想在本地调试函数,请使用 Cloud Functions Emulator。

https://cloud.google.com/functions/docs/emulator https://cloud.google.com/functions/docs/emulator

You can make it work on Visual Studio Code using Firebase functions 1.0 without having to change anything on the function code.您可以使用 Firebase 函数 1.0 使其在 Visual Studio Code 上运行,而无需对函数代码进行任何更改。 And your launch configuration seems to be correct...你的启动配置似乎是正确的......

You basically just need to set the FIREBASE_CONFIG environment variable properly when running the functions deploy command.您基本上只需要在运行functions deploy命令时正确设置 FIREBASE_CONFIG 环境变量。 Something like (don not forget to escape the " characters):类似的东西(不要忘记转义 " 字符):

FIREBASE_CONFIG="{\"databaseURL\":\"https://YOUR-FIREBASE-PROJECT.firebaseio.com\",\"storageBucket\":\"YOUR-FIREBASE-PROJECT.appspot.com\",\"projectId\":\"YOUR-FIREBASE-PROJECT\"}
functions deploy --trigger-http --timeout 600s FUNCTION_NAME

This works with Firebase Functions 1.0 because in the new version Firebase functions read it's configuration from the environment ( https://firebase.google.com/docs/functions/beta-v1-diff#new_initialization_syntax_for_firebase_admin )这适用于 Firebase 函数 1.0,因为在新版本中 Firebase 函数从环境中读取它的配置( https://firebase.google.com/docs/functions/beta-v1-diff#new_initialization_syntax_for_firebase_admin

After that, you just run normally the functions debug FUNCTION_NAME [--port] to start the function debugger and run your 'Attach' VS Code configuration.之后,您只需正常运行functions debug FUNCTION_NAME [--port]以启动函数调试器并运行您的“附加”VS Code 配置。

I wrote a little tutorial on that with more details and images: https://medium.com/@mwebler/debugging-firebase-functions-with-vs-code-3afab528bb36我写了一个关于更多细节和图像的小教程: https : //medium.com/@mwebler/debugging-firebase-functions-with-vs-code-3afab528bb36

use firebase emulators:start --inspect-functions --only functions使用firebase emulators:start --inspect-functions --only functions

more in the docs: https://firebase.google.com/docs/emulator-suite/install_and_configure文档中的更多内容: https : //firebase.google.com/docs/emulator-suite/install_and_configure

using npm ls -g --depth=0 ├── firebase-tools@9.2.2 ├── firebase@8.2.5使用npm ls -g --depth=0 ├── firebase-tools@9.2.2 ├── firebase@8.2.5

related question and answer: https://stackoverflow.com/a/65430902/965666相关问答: https : //stackoverflow.com/a/65430902/965666

previously (no longer works with the above updated versions):以前(不再适用于上述更新版本):

try: ndb firebase serve尝试: ndb firebase serve

this opens a specific Chrome browser with the debugging tools and can be a little slow to instrument all the child processes, so give it a some time.这会打开一个带有调试工具的特定 Chrome 浏览器,并且检测所有子进程可能会有点慢,所以给它一些时间。 Once running it should hit debugger breakpoints, etc.一旦运行它应该命中调试器断点等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM