从Azure IotHub / EventHub获取deviceId

问题描述 投票:0回答:2

我正在尝试从事件中心(位于IoTHub的背面)读取设备ID,但我在JS中的语法似乎错误。

module.exports = function (context, IoTHubMessages) {
context.log(`JavaScript eventhub trigger function called for message array: ${IoTHubMessages}`);

var deviceId = IoTHubMessages.SystemProperties["iothub-connection-device-id"];

该函数返回错误:异常:TypeError:无法读取未定义的属性'iothub-connection-device-id'

我不完全确定“ iothub-connection-device-id”是否是事件中心上属性的正确名称,但问题似乎出在SystemProperties上。

感谢任何帮助。

javascript azure azure-iot-hub azure-eventhub
2个回答
0
投票

首先,使用JSON.stringify打印您收到的有效负载。其次,我认为您应该可以通过执行以下操作来访问设备ID:message.annotations [“ iothub-connection-device-id”]。有关更多信息,请参考Microsoft的Github repos中提供的快速入门示例。导航到iot-hub \ Quickstarts \ read-d2c-messages文件夹,您将找到处理消息有效负载并打印输出的示例。


0
投票

您应该以这种方式阅读邮件。阅读以获取有关此主题的更多信息-https://docs.microsoft.com/en-us/samples/azure-samples/functions-js-iot-hub-processing/processing-data-from-iot-hub-with-azure-functions/

    IoTHubMessages.forEach(message => {
    context.log(`Processed message: ${message}`);
    count++;
    totalTemperature += message.temperature;
    totalHumidity += message.humidity;
    deviceId = message.deviceId;
});
© www.soinside.com 2019 - 2024. All rights reserved.