Hi I am trying to make an API request to one of my reports. Whenever I make the request I receive the following error
message: Render cycle detected. Template at /Intelli-Sense/Geo-Sense/Monthly/Headline/template was rendered previously in this render request (hierarchy: /Intelli-Sense/Geo-Sense/Monthly/Headline/template -> /Intelli-Sense/Geo-Sense/Monthly/Headline/template). Please verify that reporter.render is not causing cycle
stack: Error: Render cycle detected. Template at /Intelli-Sense/Geo-Sense/Monthly/Headline/template was rendered previously in this render request (hierarchy: /Intelli-Sense/Geo-Sense/Monthly/Headline/template -> /Intelli-Sense/Geo-Sense/Monthly/Headline/template). Please verify that reporter.render is not causing cycle at Object.callbackRequests.(anonymous function) (/var/www/html/jsreportapp/node_modules/script-manager/lib/worker-servers.js:250:31) at process.<anonymous> (/var/www/html/jsreportapp/node_modules/script-manager/lib/worker-servers.js:195:30) at process.emit (events.js:198:15) at process.EventEmitter.emit (domain.js:481:20) at emit (internal/child_process.js:848:12) at processTicksAndRejections (internal/process/task_queues.js:81:17)
When I make the API call before the POST request is made an OPTIONS request is made which returns 204 not sure if this helps.
Here is the code I wrote to use the API
$('#headline_report_now_btn').click(function() {
var request = new XMLHttpRequest();
request.open('POST', 'https://{url}/api/report', true);
request.setRequestHeader('Content-Type', 'application/json');
request.setRequestHeader('Authorization', 'Basic {token}');
request.responseType = 'blob';
request.send(JSON.stringify({
"template": {
"shortid": "H1xtsYmyWI"
},
"data": {
"user_id": "{{ current_user['id'] }}",
}
}));
$('#confirmation_modal').modal('show');
});
Here is my script code
const jsreport = require('jsreport-proxy');
const axios = require('axios');
async function beforeRender(req, res) {
if (req.context.isChildRequest) {
return
}
var user_id;
if(req.data.user_id > 0) {
user_id = req.data.user_id;
}
else {
user_id = 'null';
}
var nodemailer = require("nodemailer");
var sesTransport = require('nodemailer-ses-transport');
var transporter = nodemailer.createTransport(sesTransport({
accessKeyId: "{token}",
secretAccessKey: "{token}",
region: 'eu-west-1',
rateLimit: 5
}));
// Perform an API request to get the JWT token needed to verify future requests
const API_LOGIN = await axios.get({url}, {
headers: {
'Authorization': 'Bearer {token}'
}
});
// Perform an API request to get all the veue data needed for the reports
const API_VENUES_DATA = await axios.get({url} + user_id, {
headers: {
'Authorization': 'Bearer ' + API_LOGIN.data.JWT
}
});
for (let venue of API_VENUES_DATA.data) {
const result = await jsreport.render({
template: {
shortid: 'H1xtsYmyWI'
},
data: venue
});
// Upload and email the report
var fileName = venue.name + '_report_' + Date.now() + '.pdf'
fileName = fileName.replace(/ /g,"_");
sendEmail(fileName, transporter, venue.emails, result);
}
async function sendEmail(fileName, transporter, emails, result) {
emails.forEach(function(email) {
var mail = {
from: "{email}",
to: email,
subject: "{subject}",
text: "See the attached report",
html: `{text}`,
attachments: [
{
filename: fileName,
content: result.content
}],
}
transporter.sendMail(mail, function(error, response){
transporter.close();
if(error){
return done(error);
}
});
});
}
}
Thanks for any help in advance