0
votes

I have used the below code for converting JSON string to columns in Azure table storage.

'use strict';
// This function is triggered each time a message is received in the IoT hub.
// The message payload is persisted in an Azure storage table

module.exports = function (context, iotHubMessage) 
{
    context.log('Message received: ' + JSON.stringify(iotHubMessage));
    var date = Date.now();
    var deviceData = {
        "partitionKey": Math.floor(date / (24 * 60 * 60 * 1000)) + '',
        "rowKey": date + '',
    };

    Object.keys(iotHubMessage).forEach(function(key) {
        deviceData[key] = iotHubMessage[key];
    });

    context.bindings.outputTable = deviceData;
    context.done();
};

This still shows failure as below:

2017-10-27T06:27:02.134 Exception while executing function: Functions.fnConvertJsonToTable. Microsoft.Azure.WebJobs.Host: Error while handling parameter _binder after function returned:. Microsoft.WindowsAzure.Storage: Element 0 in the batch returned an unexpected response code. 2017-10-27T06:27:02.196 Function completed (Failure, Id=7870d04d-4cc2-4474-8ffa-d67add613f6c, Duration=145ms)

Please suggest the correct solution. Thanks in advance.

1
What do you see if you context.log outputTable?Sage

1 Answers

0
votes

I recreated this as best I could and did not receive an issues or errors;

module.exports = function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    if (req.query.name || (req.body && req.body.name)) {

        var message = req.body;
        context.log(message);

        var
            date = Date.now(),
            key = Math.floor(date / (24 * 60 * 60 * 1000));

        context.log(date);
        context.log(key);
        var tableData = {
            "partitionKey": Math.floor(date / (24 * 60 * 60 * 1000)),
            "rowKey": date
        };
        Object.keys(message).forEach(function(key) {
            tableData[key] = message[key];
        });
        context.log(tableData);
        context.bindings.outputTable = tableData;

        context.log("Output Documents Bound");

        context.res = {
            status: 200, /* Defaults to 200 */
            body: message
        };

    }
    else {
        context.res = {
            status: 400,
            body: "Please pass a name on the query string or in the request body"
        };
    }
    context.done();
};

Azure Table with Data

I noticed you had a trailing " + ''" at the end of each of your variables in deviceData, but even with those I didnt get an error.

This makes me wonder if there is something wrong with your Azure Table Storage output binding.