azure - 如何使用azure JavaScript函数应用程序在cosmos DB中插入数据?

标签 azure azure-functions azure-web-app-service azure-cosmosdb azure-function-app

我正在尝试将数据从azure javascript函数应用程序插入到cosmos DB。 这是 function.json 文件

 {
 "bindings": [
  {
  "type": "cosmosDBTrigger",
  "name": "documents",
  "direction": "in",
  "leaseCollectionName": "leases",
  "connectionStringSetting": "CosmosDBConnection",
  "databaseName": "roi",
  "collectionName": "reports",
  "createLeaseCollectionIfNotExists": "true"
  },
  {
  "type": "http",
  "direction": "out",
  "name": "res"
  }
 ]
}

index.js 有这样的代码。

module.exports = async function (context, documents) {
     if (!!documents && documents.length > 0) {
    context.log('Document Id: ', documents[0].id);
  }
 }

下面的图像将在应用程序中出现错误。 enter image description here

enter image description here

enter image description here

是否有任何函数或查询来插入数据?来自 api 的请求数据将如下所示。(它来自请求的 rawBody 属性)

enter image description here

当我获取数据时,它工作正常。

{
  "name": "inputDocumentIn",
  "type": "cosmosDB",
  "databaseName": "roi",
  "collectionName": "reports",
  "sqlQuery": "SELECT * from reports r",
  "connectionStringSetting": "CosmosDBConnection",
  "direction": "in"
}

local.setting.json 文件

{
"IsEncrypted": false,
"Values": {
 "AzureWebJobsStorage": "UseDevelopmentStorage=true",
 "AzureWebJobsDashboard": "",
 "FUNCTIONS_WORKER_RUNTIME": "node",
 "CosmosDBConnection": "AccountEndpoint=.....;"
 },
"Host": {
 "LocalHttpPort": 7071,
 "CORS": "*"
 }
}

使用 local.setting.ts 文件中的这个 azure 值,我收到以下错误。 enter image description here

最佳答案

请检查连接字符串是否指向正确的帐户,并检查是否是由防火墙和网络引起的。转到 Azure 门户上的 Cosmos 数据库帐户,然后单击“防火墙和虚拟网络”。您可以选择“所有网络”或“选定网络”并填写您当前的IP。

关于azure - 如何使用azure JavaScript函数应用程序在cosmos DB中插入数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59173802/

相关文章:

azure - 从 Azure Pipeline 运行 Maven 作业时出现 java 版本错误

azure函数无法从appsettings读取消费者组名称

azure - 使用部署槽为每个环境的 React App 提供不同的 API URL

Azure AD 注销 URL - 重定向不起作用

c++ - 通过 C++ UWP 应用程序与 Azure 应用程序服务集成

Azure API 管理 - 身份验证和授权与底层服务同步

c# - 通过 Azure Function 2.x 的 Http 触发器函数的构造函数注入(inject) ILogger

azure-functions - 在 Docker 容器上运行 Azure Function Apps Runtime 2.x 中的 csproj 函数时,如何根据环境条件设置 AuthorzationLevel?

Azure 虚拟机 - Rest API 调用不起作用

Azure 本地 git - 部署 Node.js 应用程序时出现问题