【问题标题】:How to retrieve latest record from Azure table storage using JavaScript?如何使用 JavaScript 从 Azure 表存储中检索最新记录?
【发布时间】:2020-10-22 01:19:38
【问题描述】:

Image shows the azure table如何使用 JavaScript 从 Azure 表存储中检索最新记录?

我看了这篇文章, How to retrieve latest record using RowKey or Timestamp in Azure Table storage

但我想在 Nodejs 服务器端实现相同的功能,任何建议。 到目前为止的代码,

 var dt = Date().toLocaleString();
      console.log("Got response: " + dt);

        var entGen = azure.TableUtilities.entityGenerator;
      var task = {
        PartitionKey: entGen.String('ConfigData'),
        RowKey: dt,
        DeviceId: entGen.String(req.body.devId),
      };

      // Insert the entity to the table
      tableSvc.insertEntity('configurationData', task, function (error, result, response) {
        if (!error) {
          // Entity inserted
          console.log("Added the data in table successfully");
        }
        else{
          console.log(error);
        }
      });

我得到以下错误,

name: 'StorageError',
  message: 'The \'RowKey\' parameter of value \'11/21/2017, 8:17:24 AM\' is out of range.\nRequestId:687de370-0002-0055-09a1-62d5e1000000\nTime:2017-11-21T08:17:25.6350503Z',
  code: 'OutOfRangeInput',
  statusCode: 400,
  requestId: '687de370-0002-0055-09a1-62d5e1000000' }

提前致谢。

【问题讨论】:

  • 你能告诉我们数据是什么样子的吗?您是否已经尝试过任何方法?你有什么问题吗?
  • 我试图按照我在问题中提到的文章进行操作。问题是在 C# 中,我想在 javascript 中做同样的事情
  • 数据长什么样子...是列表、数组、字典、JSON文件...?
  • 它有 4 列 PartitionKey、RowKey、TimeStamp 和 DeviceID - 都是字符串,我希望表中推送的数据始终将最新的实体放在顶部,以便我可以获取最新的实体轻松
  • 请停止在 cmets 中提供上述详细信息。建议您编辑问题并将其包含在其中。在提供这些详细信息的同时,还包括 PartitionKey 和 RowKey 中包含的示例值。另外,当您说 JavaScript 时,您是指客户端 JavaScript 还是 Node.js。此外,请包括您迄今为止编写的任何代码以及您在该代码中遇到的问题。你不能真的指望有人为你写完整的代码。

标签: javascript azure azure-table-storage


【解决方案1】:

首先,您收到错误的原因是 RowKey 值包含 / 字符,这是不允许的。对于PartitionKeyRowKey 属性的有效值,请参见此链接:https://docs.microsoft.com/en-us/rest/api/storageservices/understanding-the-table-service-data-model

现在谈到您关于如何检索最新记录的另一个问题,正如您在问题中链接的答案中所述,您需要做的是确保实体获得prepended 而不是appended。为此,您可以做的是获取当前时间的 Epoch seconds(您设置为当前 RowKey 的值并从最大 Epoch 秒数中减去该值。

例如,

var maxDate = new Date("9999-12-31");
var rowKeyValue = (maxDate.getTime() - (new Date()).getTime()).toString();
var task = {
 PartitionKey: entGen.String('ConfigData'),
 RowKey: rowKeyValue,
 DeviceId: entGen.String(req.body.devId),
};

【讨论】:

  • 感谢 Gaurav,它对我的​​应用程序非常有帮助并且工作正常
【解决方案2】:

除了上述注释之外,您还可以简单地获取纪元时间并将其从高数(100,000,000)中减去,然后将该值设置为您的 RowKey。这将反转拉回数据时返回的顶部记录。

var now = new Date()  
var secondsSinceEpoch = Math.round(now.getTime() / 1000)
var RowKey = 100000000 - secondsSinceEpoch;
public async updateEntity(table:string, body:any){
    
    var url = `${this.baseUrl}/${table}(PartitionKey='${body.PartitionKey}', RowKey='${body.RowKey}')?sv=${this.sv}`;
    var head = {
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept",
        "X-Requested-With": "XMLHttpRequest",
        "Accept": "application/json;odata=nometadata",
        "Content-Type": "application/json"
    };
    
    var method = 'PUT';        
    var res = await fetch(url, {method: method, headers: head, body: JSON.stringify(body)});         
}

var body:any = {
        PartitionKey: "My Key",
        RowKey: `${RowKey}`
    }
this.updateEntity("tableName", body);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-28
    • 1970-01-01
    • 2018-09-15
    • 1970-01-01
    • 2013-07-13
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    相关资源
    最近更新 更多