Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 从Azure表存储读取奇怪的JSON结构_Node.js_Azure Storage_Azure Functions_Azure Table Storage - Fatal编程技术网

Node.js 从Azure表存储读取奇怪的JSON结构

Node.js 从Azure表存储读取奇怪的JSON结构,node.js,azure-storage,azure-functions,azure-table-storage,Node.js,Azure Storage,Azure Functions,Azure Table Storage,我正在使用Azure函数中的Azure存储模块从Azure存储表读取数据 像这样排 var tableSvc = azure.createTableService(); var query = new azure.TableQuery().top(1000); tableSvc.queryEntities('tablename', query, null, function(error, result, response) { // work with result.entries }

我正在使用Azure函数中的
Azure存储
模块从Azure存储表读取数据

像这样排

var tableSvc = azure.createTableService();
var query = new azure.TableQuery().top(1000);
tableSvc.queryEntities('tablename', query, null, function(error, result, response) {
    // work with result.entries
}
结果对象看起来很奇怪,因为每个列值都用一个“u”键放入它自己的对象中,所以JSON看起来像这样:

{
    "PartitionKey": {
        "$": "Edm.String",
        "_": "Keyname"
    },
    "RowKey": {
        "$": "Edm.String",
        "_": "Keyname"
    },
    "Title": {
        "_": "The Item Title"
    }
}
而不是我所期望的:

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

该表在Azure存储资源管理器中看起来正常。这正常吗?或者我能以某种方式影响查询的输出吗?

这是出于设计。请参见其文档中的此示例:

这可能是因为他们有一个试图维护的类型系统,即使是在JS中

也许可以写一个方法把它抽象出来

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);
   }
}

我可能会转而使用承诺而不是回调,但这是个人的选择。

您可以将有效负载格式指定为
application/json;odata=nometadata
,然后通过
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);
    }
}