0

I'm using UiPath Orchestrator. This runs as expected. But I now additionally want to reduce the authentication to a single call (instead of always do an auth when requesting an odata). So my idea was to write the object to a file and on the odata request read that object and re-use it.

The following orchestrator object comes from the constructor of new Orchestrator. This object is ready to be used and has the following structure (via console.log(orchestrator)):

Orchestrator object structure

In my tool I need the object functions of odata. So this works:

console.log(orchestrator['v2']['odata']);

I now want to save that object as file to be able to re-use it, so I did:

fs.writeFileSync('./data.json', orchestrator, 'utf-8')

But sadly I get the error:

Converting circular structure to JSON

That is intended as the node package is using a circulare structure. So my idea was to use the circular-json package to fix that issue:

const {parse, stringify} = require('circular-json');
...
var savetofile = stringify(orchestrator);
...
var readfromfile = parse(savetofile);
...
console.log(readfromfile['v2']['odata']);

But sadly than readfromfile['v2']['odata'] is not available anymore. The reason is that stringify(orchestrator) is already minifying too heavy:

Orchestrator object structure stringified

So how I achieve that I am able to read the Orchestrator object from the file and being able to use the functions again? Or is it more useful to use a memory tool in my case?

kwoxer
  • 3,734
  • 4
  • 40
  • 70
  • Please show textual data as text, not as pictures of text. – Heretic Monkey Jan 18 '21 at 14:29
  • Basically, you can't serialize functions. You would have to create a new object and set its data to be the same as the data you saved in the file. – Heretic Monkey Jan 18 '21 at 14:30
  • Does this answer your question? [How to store a javascript function in JSON](https://stackoverflow.com/questions/36517173/how-to-store-a-javascript-function-in-json) – Heretic Monkey Jan 18 '21 at 14:31
  • I know it's usually better to have pure text instead of images. But in that case it shows better the structure within images. Read your linked article. Well that would mean a ton of work. Ok I will have a look if that solves my issue. So maybe I find a way to let it run in memory somehow meanwhile. – kwoxer Jan 18 '21 at 18:39

1 Answers1

0

The issue was not located in the Orchestrator object itself. So there is no need to do a single authentication.

My problem was that I put the res.send outside of the callback. So it never waited for the actual finish of the REST api call.

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

app.get('/jobs', function (req, res) {
  ...
  var orchestrator = require('./authenticate');
  var results = {};
  var apiQuery= {};
  orchestrator.get('/odata/Jobs', 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('/jobs', function (req, res) {
  ...
  var orchestrator = require('./authenticate');
  var results = {};
  var apiQuery= {};
  orchestrator.get('/odata/Jobs', apiQuery, function (err, data) {
    for (row in data) {
      results[i] = 
        { 
          'id' : row.id,
          ...
        };
    }
    return res.send({results});
  });  
});
kwoxer
  • 3,734
  • 4
  • 40
  • 70