2
votes

I'm reading data from my Azure Storage Table using the azure-storagemodule inside an Azure Function.

Getting the rows like this

var tableSvc = azure.createTableService();
var query = new azure.TableQuery().top(1000);
tableSvc.queryEntities('tablename', query, null, function(error, result, response) {
    // work with result.entries
}

the resulting Object looks weird as each column value is being put into it's own object with a "_" key, so the JSON looks like this:

{
    "PartitionKey": {
        "$": "Edm.String",
        "_": "Keyname"
    },
    "RowKey": {
        "$": "Edm.String",
        "_": "Keyname"
    },
    "Title": {
        "_": "The Item Title"
    }
}

instead of what I would expect:

{
    "PartitionKey": "Keyname",
    "RowKey": "Keyname",
    "Title": "The Item Title"
}

The table looks normal in the Azure Storage Explorer. Is this normal? Or can I somehow influence the output of the query?

2

2 Answers

7
votes

You could specify the payload format as application/json;odata=nometadata and then get the resulting Object via response.body.value.

var options = { payloadFormat: "application/json;odata=nometadata" };
tableSvc.queryEntities('tablename', query, null, options, function(error, result, response) {
    if(!error) {
        console.log(response.body.value);
    }
}
1
votes

This is by design. See this example in their docs: https://docs.microsoft.com/en-us/azure/cosmos-db/table-storage-how-to-use-nodejs#add-an-entity-to-a-table

This is likely because they have a type system they are trying to maintain, even in JS.

Maybe write a method to abstract that away for you?

function getFromTable(cb) {
   var tableSvc = azure.createTableService();
   var query = new azure.TableQuery().top(1000);
   tableSvc.queryEntities('tablename', query, null, function(error, result, response) {
       var response = {};
       for(var item in result) {
           if(result.hasOwnProperty(item)) {
               response[item] = result[item]["_"];
           }
        }
        cb(response);
   }
}

I'd probably move to using Promises instead of callbacks, as well, but that's a personal choice.