我已經創建了一個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