0

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.

Matthijs van der Veer
  • 3,865
  • 2
  • 12
  • 22
Gcobza
  • 432
  • 1
  • 5
  • 12

1 Answers1

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: cardinality of the binding

Matthijs van der Veer
  • 3,865
  • 2
  • 12
  • 22
  • Thanks Matthijs so much, now i know in future if i encounter the function problem on json file. Node js will complain as a single entity, meaning if my cardinality is one i am likely to get this message often up until i modified as many on my Integrate tab as well Json function file. – Gcobza Aug 15 '19 at 06:58