Issue migrating from JSReport 1.10.1 to 2.11.0
-
Hi,
I'm upgrading a script from JSReport 1.10.1 (with node 8.15.1) to JSReport 2.11.0 (with node 12.22.1).The 1.10.1 script begins like this and then continues with running queries to extract data from an Elasticsearch index for output to a CSV file.
(function() { 'use strict'; var config = require('Configuration.json'); var elasticsearch = require('elasticsearch'); var esClient = new elasticsearch.Client({ host: config.esURL, log: { "type": config.esLog.type, "level": config.esLog.level, "path": config.esLog.path } }); // simple test for ES connectivity - fetch cluster health esClient.cluster.health({},function(err,resp,status) { console.log("-- Client Health -- resp",resp); console.log("-- Client Health -- err",err); console.log("-- Client Health -- status",status); });
That works and returns data from Elasticsearch telling me the status of the Elasticsearch cluster; knowing the cluster is healthy we then proceed to run a query to get the data from an Elasticsearch index that's needed for the output file. And that's all good and it works fine in 1.10.1.
In 2.11.0, I've modified it slightly to this:
function beforeRender(req, res, done) { var config = require('Configuration.json'); var elasticsearch = require('@elastic/elasticsearch'); var esClient = new elasticsearch.Client({ node: config.esURL, auth: { username: config.user, password: config.password } }); // simple test for ES connectivity - fetch cluster health esClient.cluster.health({},function(err,resp,status) { console.log("-- Client Health -- resp",resp); console.log("-- Client Health -- err",err); console.log("-- Client Health -- status",status); });
Here the call to get the Elasticsearch cluster health information seems to do nothing; apparently it doesn't run. And that's my issue; I don't know why this is behaving differently in 2.11.0.
So far I've been unable to get anything back from the Elasticsearch client using either this type of request or a query to get data.
console log shows that esClient is looking correct; and if I use invalid values when I define esClient (e.g. a URL that's not valid) then I do get error messages back. So defining esClient seems to be working ok.
These are the configuration options used with 2.11.0 to start JSReport:
var jsReportConf = { 'httpPort': '3100', 'store': { 'provider': 'fs' }, 'blobStorage': { 'provider': 'fs' }, 'templatingEngines': { 'numberOfWorkers': 2, 'reportTimeout': 180000, 'allowedModules': '*' }, 'extensions': { 'scripts': { 'timeout': 180000 }, 'express': { 'renderTimeout': 600000 }, }, 'reportTimeout': 180000, 'logger': { 'console': { 'transport': 'console', 'level': 'debug' }, 'file': { 'transport': 'file', 'level': 'debug', 'filename': 'logs/JSReport-reporter.log' }, 'error': { 'transport': 'file', 'level': 'error', 'filename': 'logs/JSReport-error.log' } }, 'allowLocalFilesAccess': true };
The Elasticsearch package I'm using has changed as well (from elasticsearch 16.5 to @elastic/elasticsearch 7.12) but outside JSReport I'm able to use node.js to run essentially the code same as I've shown here and it gets the data from Elasticsearch without any problems.
I've read the breaking changes documentation for 2.0 but assume I've missed (or misunderstood) something, how would I get this to work in 2.11.0 the same way that it works with 1.10.1 ?
Thanks,
-Derek
-
hi @dmt876
function beforeRender(req, res, done) { var config = require('Configuration.json'); var elasticsearch = require('@elastic/elasticsearch'); var esClient = new elasticsearch.Client({ node: config.esURL, auth: { username: config.user, password: config.password } }); // simple test for ES connectivity - fetch cluster health esClient.cluster.health({},function(err,resp,status) { console.log("-- Client Health -- resp",resp); console.log("-- Client Health -- err",err); console.log("-- Client Health -- status",status); });
the script in 2.11.0 has two forms, one is based on promises and async/await style, and the other is based on the callback style. to use one form or the other depends on how you define the
beforeRender
function.you are currently defining the
beforeRender
with 3 arguments, which means you are choosing the callback style, and when you use the callback style you need to use that third paramdone
to signal the script that it finished.for example, I know that your script is longer than just this fragment of code, but I will update it to reflect the usage of the done parameter.
function beforeRender(req, res, done) { var config = require('Configuration.json'); var elasticsearch = require('@elastic/elasticsearch'); var esClient = new elasticsearch.Client({ node: config.esURL, auth: { username: config.user, password: config.password } }); // simple test for ES connectivity - fetch cluster health esClient.cluster.health({},function(err,resp,status) { console.log("-- Client Health -- resp",resp); if (err) { console.log("-- Client Health -- err",err); return done(err) } console.log("-- Client Health -- status",status); done() }); }
you should update the rest of your code to properly call
done
either with an error or with no param when it is completed