2

Image shows the azure tableHow to retrieve latest record from Azure table storage using JavaScript?

I saw the article, How to retrieve latest record using RowKey or Timestamp in Azure Table storage

but i want to implement the same in Nodejs server side, Any suggestion. code so far,

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

I got below 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' }

Thanks in advance.

  • Can you show us how the data looks like? Have you tried any method already? Do you have any issue? – Adriano Nov 21 '17 at 04:36
  • i tried to follow the article i have mentioned in the question. Problem is that is in C# and i want to do the same in javascript – Vishal Malaviya Nov 21 '17 at 04:37
  • How does the data look like... Is it a list, an array, a dictionary, a JSON file...? – Adriano Nov 21 '17 at 05:49
  • It has 4 columns PartitionKey, RowKey, TimeStamp and DeviceID - all are string i want that the pushed data in the table shall always put the latest entity on the top, so that i can fetch the latest one easily – Vishal Malaviya Nov 21 '17 at 06:51
  • Please stop providing details like above in comments. It is recommended that you edit the question and include them there. While providing these details, also include sample value contained in PartitionKey and RowKey. Also when you say JavaScript, do you mean client side JavaScript or Node. Furthermore, please include any code that you have written so far and the issues you're encountering in that code. You can't really expect someone to write entire code for you. – Gaurav Mantri Nov 21 '17 at 07:00
  • Added the details for the question. @Gaurav, Thanks for the prompt response, I never said i want full code just asked if someone has any idea regarding the implementation. – Vishal Malaviya Nov 21 '17 at 08:41
  • Thank you for including the details. I have provided an answer. HTH. – Gaurav Mantri Nov 21 '17 at 08:55

2 Answers2

2

First, the reason you're getting the error is because the RowKey value contains / character, which is not allowed. For the valid values for PartitionKey and RowKey attributes, please see this link: https://learn.microsoft.com/en-us/rest/api/storageservices/understanding-the-table-service-data-model.

Now coming to your other question about how to retrieve the latest record, as mentioned in the answer you linked in your question what you would need to do is ensure that the entities get prepended instead of appended. To do so what you could do is get the Epoch seconds for the current time (the value you're setting as current RowKey and subtract that value from max Epoch seconds.

For example,

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 Mantri
  • 128,066
  • 12
  • 206
  • 241
0

In addition to the above comment, you could also simply get the epoch time and subtract it from a high number (100,000,000) and set that value as your RowKey. This will inverse the top records returned when pulling back data.

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);
Ostrava
  • 95
  • 1
  • 9