Warm tip: This article is reproduced from serverfault.com, please click

Disable possible caching of Node.js Orchestrator to return always latest data

发布于 2021-01-19 08:07:28

I want to get the latest data via odata. So https://uipathspace.com/odata/Robots returns all the robots.

But when I change for instance my robot description:

enter image description here

and refresh https://uipathspace.com/odata/Robots. It shows correctly the updated text:

enter image description here

But I created an application using the node.js Orchestrator implementation. Here I cannot see the description changes:

enter image description here

Is there something hardcore cached and if so how to disable it? I am not able to find that setting.

Questioner
kwoxer
Viewed
0
kwoxer 2021-01-20 20:36:17

The problem is that the res.send is put outside of the callback. So it never waits for the actual finish of the REST api call.

This is the base code where it just took the static result of the first request, it never updated the results:

app.get('/robots', function (req, res) {
  ...
  var orchestrator = require('./authenticate');
  var results = {};
  var apiQuery= {};
  orchestrator.get('/odata/Robots', apiQuery, function (err, data) {
    for (row in data) {
      results[i] = 
        { 
          'id' : row.id,
          ...
        };
    }
  });  
  return res.send({results});
});

The solution is to moving the res.send({results}); into the orchestrator.get, then it properly overwrites the results as it waits correctly for the callback:

app.get('/robots', function (req, res) {
  ...
  var orchestrator = require('./authenticate');
  var results = {};
  var apiQuery= {};
  orchestrator.get('/odata/Robots', apiQuery, function (err, data) {
    for (row in data) {
      results[i] = 
        { 
          'id' : row.id,
          ...
        };
    }
    return res.send({results});
  });  
});