7

VS Code集成终端我运行firebase serve --only functions,hosting 然后在我创建的默认launch.json调试选项卡:功能调试

{ 
    "version": "0.2.0", 
    "configurations": [ 
    { 
     "type": "node", 
     "request": "launch", 
     "name": "Launch Program", 
     "program": "${file}" 
    } 
    ] 
} 

我要调试的服务器侧(功能/ index.js)而不是客户端。

我试过https://code.visualstudio.com/docs/nodejs/nodejs-debugging一些配置没有运气。

如何调试VS代码中的Firebase函数?

+0

通过单元测试进行调试可能是您现在的最佳选择:https://firebase.google.com/docs/functions/unit-testing –

+0

不是首选选项,但是如果是唯一的工作,我会与。 –

回答

3

如果不先定义Firebase配置变量,则无法调试Firebase函数。 Firebase CLI为您提供帮助。

要进行调试,您可以尝试与Firebase功能单元测试相同的技巧。

  1. 安装:

    admin.initializeApp = function() {} 
    functions.config = function() { 
        return { 
         firebase: { 
          databaseURL: 'https://not-a-project.firebaseio.com', 
          storageBucket: 'not-a-project.appspot.com', 
         } 
        }; 
    } 
    

    您可以在相同的方式与任何其他谷歌的云功能现在调试火力地堡功能:以下行index.js文件打电话之前admin.initializeApp(functions.config().firebase)

    添加云功能模拟器:

    npm install -g @google-cloud/functions-emulator 
    
  2. St艺术模拟器:

    functions start 
    
  3. 部署您的功能:

    functions deploy helloWorldFunction --trigger-http 
    

    你会得到的输出是这样的:

    Waiting for operation to finish...done. 
    Deploying function........done. 
    Function helloWorldFunction deployed. 
    
    Property | Value 
    ---------|------------------------------------------------------------------------ 
    Name  | helloWorldFunction 
    Trigger | HTTP 
    Resource | http://localhost:8010/helloWorldProject/us-central1/helloWorldFunction 
    
  4. 要调试使用标准Node.js的调试类型:

    functions debug helloWorldFunction 
    

    您将获得:

    Debugger for helloWorldFunction listening on port 5858. 
    
  5. 现在在你的VS代码以下行添加到您的launch.json VS代码

    { 
        "version": "0.2.0", 
        "configurations": [ 
         { 
          "name": "Node.JS (local)", 
          "type": "node", 
          "request": "attach", 
          "port": 5858 
         } 
        ] 
    } 
    
  6. 开始调试,并通过调用URL你有触发功能在步骤#3。

    您也可以通过在终端中输入functions call helloWorldFunction来触发该功能。

欲了解更多详情,请参阅这里Cloud Functions Local Emulator的说明。

+0

尝试部署时出现错误。我在谷歌搜索,没有任何帮助,我放弃了。 –

+0

嗨安德鲁!你的回答非常棒,但为了使它工作,我必须做一些修正。首先,应该删除行“admin.initializeApp = function(){}”;它实际上使下一次调用admin.initializeApp无用。其次,请澄清,nota-a-project.firebaseio.com应该包含您的Firebase应用的正确地址;对于非项目名称而言,它有点模棱两可。最后,在functions.config中的firebase对象中,我必须设置'credential:admin.credential.applicationDefault()'。通过这些修改,我能够运行仿真器并在VSCode上进行调试 –

+0

当您已经安装了Firebase功能仿真器时,您建议安装并使用GCP功能仿真器,我感到有些惊讶。我假设它们几乎完全相同,但是在使用FB函数时使用FB函数工具/仿真器更有意义。 – Tom