IOTHubMessage.forEach is not a function?

狂风中的少年 提交于 2021-01-29 08:04:17

问题


Can anyone tell me how to parse this problem?

I have an thrown error message that when i create azure cosmossDB, my cosmosDB output binding thrown message that IOTHubMessage.forEach is not a function;

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

    var count = 0;
    var totalTemperature = 0.0;
    var totalHumidity = 0.0;
    var deviceId = "*****";

    IoTHubMessages.forEach(message => {
        context.log(`Processed message: ${message}`);
        count++;
        totalTemperature += message.temperature;
        totalHumidity += message.humidity;
        deviceId = message.deviceId;
    });

    var output = {
        "deviceId": deviceId,
        "measurementsCount": count,
        "averageTemperature": totalTemperature / count,
        "averageHumidity": totalHumidity / count
    };

    context.log('Output content: ${output}');
    context.bindings.outputDocument = output;


    context.done();
};

What am i missing? Please assist, thanks.


回答1:


It wasn't included in your answer, but the issue is most likely in your functions.json file. The bindings for IoTHub by default only handle one message at a time. This means that your IoTHubMessages isn't an array, but a single object. You need to change cardinality from one to many.

To change this, edit your functions.json file to include a cardinality property.

{
  "type": "eventHubTrigger",
  "name": "eventHubMessages",
  "direction": "in",
  "eventHubName": "MyEventHub",
  "cardinality": "many",
  "connection": "myEventHubReadConnectionAppSetting"
}

In case you made this function in the portal, you can change the cardinality of the binding in the Integrate part of the function:



来源:https://stackoverflow.com/questions/57497522/iothubmessage-foreach-is-not-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!