Hi,
jsReport: 1.10.0
I've hit a curious problem but I think the problem is the lack of understanding of how the done() callback works.
I have a requirement for a report that requires to gather the data from multiple rest endpoints (multiple async requests).
- first I obtain the token from the token service.
- then I pass the token and the "done" function to the function that gets the data from other service
That works ok.
But if I, in the step 2, add another request simultaneously, I'm unable to combine the results as I hit the ECONNRESET.
This other request, if I omit the request in step 2, proceeds fine so both requests are fully valid and authenticated requests.
Any ideas on how can I combine the multiple request and call done() as soon as it's done?
In the sample below I've deleted some parts so might have some issues, but basically I would like to wait for once the both requests are finished, I'd liket to do some checks and then call done.
var request = require('request');
var https = require('https');
var http = require('http');
var proxiedRequest = request;
var data = {
areas: null,
fms: null
};
function beforeRender(req, res, done) {
console.log('-------------------- INIT ------------------------');
req.data = req.data || {};
try {
getToken(req, done);
}
catch (err) {
console.log('----- ERROR ----');
done(err)
}
}
function getToken(req, done) {
var options = {
method: 'POST',
url: '.../getToken',
headers: {
'content-type': 'application/x-www-form-urlencoded',
'Cache-Control' : 'no-cache'
},
form: {
...
},
timeout: 10000
};
console.log('sending the request');
proxiedRequest(options, function(error, response, body) {
console.log(body.token);
getAGSData(JSON.parse(body).token, req, done);
}, function (err) {
console.log('Error while getting the token: ', err);
});
}
function getAGSData(token, req, done) {
console.log('--------------------------- AGOL Data Retreival ------------------------');
if (!token) {
done({
error: 'No token provided.'
});
// Not necessary I reckon. I don't know. write it the first time.
return;
}
try {
var query = '1=1 AND TYPE=\'RAMP\'';
console.log('Querying the data with the query: ', query);
// Make the request
proxiedRequest({
method: 'GET',
url: '.../FeatureServer/0/query',
qs: {
f: 'json',
where: query,
outfields: '*',
returnGeometry: false,
token: token,
orderByFields: 'LABEL'
},
headers: {
'Cache-Control' : 'no-cache'
}
},
(error, response, body) => {
console.log('------------- AGOL Response -----------');
if (error) {
console.error(error);
done('Error');
}
var features = JSON.parse(body).features || [];
req.data.mydata = features;
data.areas = features;
console.log(JSON.stringify(data));
done();
}
);
/*proxiedRequest(
{
method: 'GET',
url: '.../other/FeatureServer/0/query',
rejectUnauthorized: false,
qs: {
f: 'json',
where: '1=1',
outfields: '*',
returnGeometry: false
}
},
(error, response, body) => {
console.log('------------- BMAGISWEB Response FMS: -----------');
if (error) {
console.error(error);
console.log(response);
console.log(body);
done('Error');
return;
}
var features = JSON.parse(body).features;
req.data.mydata = features;
// console.log(error);
// console.log(response);
console.log(JSON.parse(body).features);
data.fms = features;
console.log(JSON.stringify(data));
done();
}
);*/
} catch (err) {
console.error('Caught Error: ', error);
done();
}
}