我已经创建了一个Azure函数来保存Iot Hub中的SQL数据库中的数据,并且工作正常,现在我想将异常和错误保存到Azure存储表中,因此我添加了try{ } catch(err){}
但这不起作用。请纠正我。谢谢!如何从Azure函数js中存储异常/错误?
我的功能就是在这里
module.exports = function (context, iotHubMessage) {
try {
var strMsg = JSON.stringify(iotHubMessage);
context.log('Message received: ' + strMsg);
var ob1 = { "employee_idw": 444, "last_name": "Teller", "first_name": "Saara", "age": 34, "salary": 87000 };
//I misspelled 'employee_idw' to generate error
var ob2 = { "employee_id": 555, "last_name": "Teller", "first_name": "Saara", "age": 31, "salary": 87000 };
ob1.EventProcessedUtcTime = new Date;
ob2.EventProcessedUtcTime = new Date;
var arr = [];
arr.push(ob1);
arr.push(ob2);
context.bindings.outputTable = arr;
context.done();
} catch (err) {
context.log('CCC Error' + err); // even can not see this message in log
context.bindings.error= { "partitionKey": partitionKey, "rowKey": rowKey, "data": err };
}
};
看到这是JSON文件
{
"bindings": [
{
"type": "eventHubTrigger",
"name": "myEventHubMessage",
"path": "myeventhub",
"consumerGroup": "$Default",
"connection": "PBCorIOTHub_events_IOTHUB",
"cardinality": "many",
"direction": "in"
},
{
"type": "apiHubTable",
"name": "outputTable",
"dataSetName": "default",
"tableName": "employees",
"connection": "sql_SQL",
"direction": "out"
},
{
"type": "table",
"name": "error",
"tableName": "dddtTest",
"connection": "cccteststr_STORAGE",
"direction": "out"
}
],
"disabled": false
}
为什么拼写错误属性会在函数内部产生错误?对于表格存储,您只需要第二个输出绑定,并在'catch'块中分配它。 – Mikhail
'context.bindings.outputTable = arr'这是我的SQL表,所以我认为它会产生错误,我想将这个错误保存在Azure存储表中。 –
它为什么会产生错误?这是一个JS变量赋值给另一个变量。写入SQL会在函数退出后异步发生。 – Mikhail