【问题标题】:Retrieve properties of Service Bus message in Node.js Azure Function在 Node.js Azure 函数中检索服务总线消息的属性
【发布时间】:2018-03-29 21:10:00
【问题描述】:

我使用 Azure 服务总线将 IoT 中心与 Node.js 函数进行通信。在函数方面,我需要访问消息正文和自定义属性。

通过在别处检索消息,我注意到我在服务总线中的消息包括:

  1. 正文(物联网设备端设置的字符串)。
  2. 自定义属性(包括在 IoT 设备端设置的属性、IoT 设备 ID 和一些其他元数据)。

我在网上发现在 C# 中可以使用 BrokeredMessage 对象访问这些自定义属性。但是,没有提及如何在 Node.js 中实现这一点。为了提供一些详细信息,我在消息到达 Function 后立即使用以下代码打印消息:

module.exports = function(context, message) {
    context.log('Message', message, 'of type', (typeof message));
    ...
}

我在日志控制台中得到的是:

消息 { test: true } 对象类型

其中“{ test: true }”是物联网设备设置的消息内容。虽然没有任何属性的痕迹......

是否有一些正式的方式或至少是一种技巧来接收和提取这些属性?

谢谢!

【问题讨论】:

标签: node.js azure-functions azureservicebus azure-iot-hub azure-servicebus-queues


【解决方案1】:

浏览 Ling Toh 在评论中发布的链接中提到的资源,我受到启发,想看看上下文对象。

显然,context.bindingData.properties 对象中提供了所有服务总线自定义属性。

就我而言:

properties: 
{ 
    type: 'sometype',   // <- this is the property I have set manually in IoT Hub message
    'iothub-connection-device-id': 'mydeviceid',
    'iothub-connection-auth-method': '{"scope":"somescope","type":"sometype","issuer":"external","acceptingIpFilterRule":null}',
    'iothub-connection-auth-generation-id': 'someid' // <- These are added by IoT Hub
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-30
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-06
    • 2021-11-14
    相关资源
    最近更新 更多