Error: jsreport already initialized or just initializing. Make sure init is called only once
-
Hi,
So I use jsreport to create a pdf from a template. So we prep the data and require in the html template then do the following:
jsreport.init().then(() => { return jsreport.render({ template: { content: htmlTemplate, engine: 'handlebars', recipe: 'chrome-pdf', chrome: { format: 'A4', printBackground: true, marginTop: '1in', marginRight: '0.5in', marginBottom: '0.25in', marginLeft: '0.5in' } }, data: modifiedReport }).then(resp => { return MailService.mailer(resp.content, 'ServiceReport.pdf', reportId, type); }); }).catch(err => { console.error(err); });
this is how I import it:
const jsreport = require('jsreport-core')();
these are the packages I have:
"jsreport-chrome-pdf": "^1.3.0", "jsreport-core": "^2.4.3", "jsreport-handlebars": "^2.0.1", "puppeteer": "^1.13.0",
I read you did not need to get jsreport to use those modules by calling use()
In development all is good. On the aws ec2 debian linux server it errors with
Error: jsreport already initialized or just initializing. Make sure init is called only once at Reporter.init (/home/ben/node/node_modules/jsreport-core/lib/reporter.js:99:29) at createJSReportPdf (/home/paul/node/pdfReport/composeServiceReports.js:49:12) at Object.sendReportEmail (/home/paul/node/pdfReport/composeServiceReports.js:16:10) at process._tickCallback (internal/process/next_tick.js:68:7)
This seems to happen after jsreport has been called once.
Any ideas of how to tackle this?
Much appreciated, thanks
Paul
-
Call
jsreport.init
just once at the beginning of your application.
Store the jsreport instance.
And later use justjsreport.render
.
-
Hi! Thanks for getting back to me. Like this:
const jsreport = require('jsreport-core')(); let jsReportInitialized = false;
..........
function checkIfJsReportIsInit() { if( !jsReportInitialized ) { jsReportInitialized = true; return jsreport.init(); } return Promise.resolve(); }
............
return checkIfJsReportIsInit().then(() => { jsreport.render({ template: { content: htmlTemplate, engine: 'handlebars', recipe: 'chrome-pdf', chrome: { format: 'A4', printBackground: true, marginTop: '1in', marginRight: '0.5in', marginBottom: '0.25in', marginLeft: '0.5in' } }, data: modifiedReport }).then(resp => { return MailService.mailer(resp.content, 'ServiceReport.pdf', reportId, type); }); }).catch(err => { console.error(err); });
-
you are probably just missing one return at
return jsreport.render({
the rest looks good
-
Thanks @jan_blaha , really appreciate it.