问题
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