簡體   English   中英

調試 firebase 雲函數

[英]Debugging firebase cloud functions

如何使用 Visual Studio 代碼firebase-database觸發函數進行調試? 我嘗試了模擬器,但是當我調用它時出現錯誤

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

這段代碼我想調試

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

這是我的啟動文件

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

debug-agent僅用於遠程調試。 如果您想在本地調試函數,請使用 Cloud Functions Emulator。

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

您可以使用 Firebase 函數 1.0 使其在 Visual Studio Code 上運行,而無需對函數代碼進行任何更改。 你的啟動配置似乎是正確的......

您基本上只需要在運行functions deploy命令時正確設置 FIREBASE_CONFIG 環境變量。 類似的東西(不要忘記轉義 " 字符):

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

這適用於 Firebase 函數 1.0,因為在新版本中 Firebase 函數從環境中讀取它的配置( https://firebase.google.com/docs/functions/beta-v1-diff#new_initialization_syntax_for_firebase_admin

之后,您只需正常運行functions debug FUNCTION_NAME [--port]以啟動函數調試器並運行您的“附加”VS Code 配置。

我寫了一個關於更多細節和圖像的小教程: https : //medium.com/@mwebler/debugging-firebase-functions-with-vs-code-3afab528bb36

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

文檔中的更多內容: https : //firebase.google.com/docs/emulator-suite/install_and_configure

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

相關問答: https : //stackoverflow.com/a/65430902/965666

以前(不再適用於上述更新版本):

嘗試: ndb firebase serve

這會打開一個帶有調試工具的特定 Chrome 瀏覽器,並且檢測所有子進程可能會有點慢,所以給它一些時間。 一旦運行它應該命中調試器斷點等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM