繁体   English   中英

无法使用 Visual Studio Code 调试 Azure JavaScript Function

[英]Can't Debug Azure JavaScript Function using Visual Studio Code

我第一次使用 VS 代码并尝试使用 JavaScript 语言创建 azure function。 我关注了这篇文章

环境:

  • 安装了 NodeJS v16.x
  • Azure 已安装功能扩展
  • Azure 已安装 Functions Core Tools 4.x

问题:

我创建了一个示例 azure function。当使用 f5 运行应用程序时,它显示以下错误

错误图片 1

我从host.json中删除了扩展包配置,然后重试。 这次,我收到以下错误

错误图片2

谁能帮我这个

更新:

主机.json

    {
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[2.*, 3.0.0)"
  }
}

local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "node"
  }
}

索引.js

module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    const name = (req.query.name || (req.body && req.body.name));
    const responseMessage = name
        ? "Hello, " + name + ". This HTTP triggered function executed successfully."
        : "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.";

    context.res = {
        // status: 200, /* Defaults to 200 */
        body: responseMessage
    };
}

这是我按照问题中给出的相同文档使用 Visual Studio Code 运行 JavaScript Stack 的 Azure 函数的解决方法

先决条件:

  1. 安装VS Code并安装Azure 帐户Azure 功能Azure 存储Azurite 、扩展菜单中的扩展以及从 VS Code登录Azure 帐户。
  2. 下载并安装NodeJS版本 14(我猜 16 是预览版),来源: https://nodejs.org/en/download/https://phoenixnap.com/kb/install-node.js-npm-on -视窗
  1. 单击独立安装程序从此处安装Azure 存储模拟器(已弃用但仍用于本地开发和测试)。 - 安装 Azure 存储模拟器后,确保它正在运行。 如果它已关闭,则手动运行它。 要启动 Azure 存储模拟器:

    • Select开始按钮或按Windows键。
    • 开始输入Azure Storage Emulator
    • Select 显示的应用程序列表中的模拟器。
    • 键入Start > Enter 并关闭 Window。
  2. 安装用于旧版本和新版本堆栈开发的Azure Functions Core Tools v3 和 v4,并在命令提示符下使用func --versio n 命令检查版本。

  3. 安装Azure CLI ,从 安装PowerShell 模块

  4. 在转到 IDE (VS Code/Visual Studio) 之前,请遵循此先决条件检查


使用/不使用调试运行 Azure 函数的步骤:

  1. 在文件资源管理器中创建工作区文件夹并从该文件夹路径打开 VS Code。
  2. 使用 Azure 功能扩展创建 Azure 功能项目 JavaScript 堆栈。 这是未修改的样板代码:

索引.js

module.exports = async  function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');

const  name = (req.query.name || (req.body && req.body.name));

const  responseMessage = name
? "Hello, " + name + ". This HTTP triggered function executed successfully."
: "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.";

context.res = {
// status: 200, /* Defaults to 200 */
body:  responseMessage
};
}

local.settings.json

{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "node"
}
}

主机.json

{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[2.*, 3.0.0)"
}
}
  • 无需从host.json中删除扩展包

要运行 Function、go 到运行菜单 > Select 选项Start DebuggingRun without Debugging ,我在没有启动 VS 代码的情况下运行它,它在本地成功运行。

在此处输入图像描述

调试代码:

在代码的任意行和 select 中放置一个断点从Run菜单Start Debugging选项

在此处输入图像描述

注意:对于类似的错误: A host error has occurred during startup operation , Value cannot be null ,几个解决步骤是:

  • 在本地开发和测试目的中使用Azure 存储模拟器
  • 确保您拥有最新版本的 Azure Functions Core Tools以及旧版本(您可以安装多个版本的 AF Core Tools 并根据需要使用)。
  • local.settings.json中, AzureWebJobsStorage的值默认为空,但如果我们在本地使用该项目,最好使用 like "AzureWebJobsStorage": "UseDevelopmentStorage=true"

您可以从我的GitHub Repository下载上述项目。


暂无
暂无

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

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