问题描述
有人可以告诉我如何解析此问题吗?
Can anyone tell me how to parse this problem?
我有一条抛出的错误消息,即当我创建天蓝色的cosmossDB时,我的cosmosDB输出绑定抛出的消息是IOTHubMessage.forEach不是函数;
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.
推荐答案
它没有包含在您的答案中,但是问题很可能出现在您的functions.json文件中.默认情况下,IoTHub的绑定一次只能处理一条消息.这意味着您的IoTHubMessages不是数组,而是单个对象.您需要将基数从一个更改为许多.
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.
要更改此设置,请编辑functions.json文件以包含基数属性.
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"
}
如果您在门户网站中创建了此功能,则可以在该功能的 Integration 部分中更改绑定的基数:
In case you made this function in the portal, you can change the cardinality of the binding in the Integrate part of the function:
这篇关于IOTHubMessage.forEach不是函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!