从 Azure Table 存储中读取的奇怪 JSON 结构

Weird JSON structure reading from Azure Table Storage

我正在使用 Azure 函数中的 azure-storage 模块从我的 Azure 存储 Table 读取数据。

像这样获取行

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

生成的对象看起来很奇怪,因为每个列值都使用“_”键放入它自己的对象中,因此 JSON 看起来像这样:

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

而不是我所期望的:

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

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

这是设计使然。在他们的文档中查看此示例:https://docs.microsoft.com/en-us/azure/cosmos-db/table-storage-how-to-use-nodejs#add-an-entity-to-a-table

这可能是因为他们有一个他们试图维护的类型系统,即使在 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);
   }
}

我可能也会转向使用 Promises 而不是回调,但这是个人选择。

您可以将负载格式指定为 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);
    }
}