javascript - 来自 CosmosDB JavaScript 的 Azure 函数更改源

标签 javascript azure azure-functions azure-cosmosdb azure-iot-hub

我想在 Azure 中实现以下场景。

模拟 IoT 设备向 IoT 中心发送一些消息,并将其存储在 CosmosDB 中。到目前为止,这件事正在发挥作用。我使用 Azure Function 来实现这一目标。

下一步是使用 CosmosDB 中的更改源触发器创建新的 Azure 函数,并将其推送到 Azure 事件中心。

我在门户中创建了适当的function.json

{
  "bindings": [
    {
      "type": "eventHub",
      "name": "outputEventHubMessage",
      "connection": "dev-euwest_all_EVENTHUB",
      "eventHubName": "forwardedmessages",
      "direction": "out"
    },
    {
      "type": "cosmosDBTrigger",
      "name": "inputDocuments",
      "connectionStringSetting": "cdb-euwest_DOCUMENTDB",
      "databaseName": "iot",
      "collectionName": "messages",
      "leaseCollectionName": "forwardedmessages",
      "createLeaseCollectionIfNotExists": true,
      "direction": "in"
    }
  ],
  "disabled": false
}

然后我有index.js

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

当我运行它时,没有失败,但我没有看到任何文档 ID。

这是我使用 Azure IoT Python 设备 Azure Python Simulator 的示例源消息

{
    "_id" : ObjectId("5da47cd2b19c571e106a040c"),
    "temperature" : 34.78470937343868,
    "humidity" : 79.96610016503291
}

我在这里做错了什么?如何正确拾取消息?

最佳答案

示例文档是一个 Bson 文档。这意味着您正在使用带有 Mongo API 的 Cosmos DB 帐户。​​

如果正确,请检查official documentation ,其中指出:

Currently, the Azure Functions trigger for Cosmos DB is supported for use with the Core (SQL) API only.

此外,在Azure Functions Binding documentation中,它指出:

Azure Cosmos DB bindings are only supported for use with the SQL API. For all other Azure Cosmos DB APIs, you should access the database from your function by using the static client for your API, including Azure Cosmos DB's API for MongoDB, Cassandra API, Gremlin API, and Table API.

目前触发器不支持Mongo。

关于javascript - 来自 CosmosDB JavaScript 的 Azure 函数更改源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58394359/

相关文章:

javascript - 我如何设置一个方法来更新 JavaScript 对象传递的属性?

.net - 服务结构: Use Refresh Mode for Faster Debugging

azure-functions - Angular 7 PWA + Azure Functions Gateway Timeout 504 仅使用 Https

powershell - 如何使用 Function Apps 在 Azure 中使用 PowerShell 登录 AzureRmAccount

azure - 有没有办法为azure云服务或azure VM创建混合连接?

azure - 错误索引方法 FunctionName 未解析为值

JavaScript 删除数组

javascript - ExecJS::RuntimeError <%= javascript_include_tag 'application' , 'data-turbolinks-track' => true %>

javascript - 如何在 ion-slide-box 上隐藏点

Fedora-33 上的 git(1) CLI 在创建 SSH key 对并将其添加到 Azure DevOps(或 GitHub)后请求密码 -(步骤如下)