<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Report call our api beforeRender for only one report]]></title><description><![CDATA[<p>Hi,</p>
<p>Can a jsreport template call our api to pull data instead of having to push data to it?</p>
<p>I understand that there is the beforeRender event that runs in nodeJS with all templates, how can I have it executed for only one template because the call will be different based on input parameters and context.</p>
<p>Also is there a way to include scripts to the beforeRender so that they simply call our api instead of copy pasting code?</p>
]]></description><link>https://forum.jsreport.net/topic/252/report-call-our-api-beforerender-for-only-one-report</link><generator>RSS for Node</generator><lastBuildDate>Tue, 19 May 2026 03:28:32 GMT</lastBuildDate><atom:link href="https://forum.jsreport.net/topic/252.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 26 Sep 2017 17:46:09 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Report call our api beforeRender for only one report on Tue, 26 Sep 2017 17:47:22 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>Can a jsreport template call our api to pull data instead of having to push data to it?</p>
<p>I understand that there is the beforeRender event that runs in nodeJS with all templates, how can I have it executed for only one template because the call will be different based on input parameters and context.</p>
<p>Also is there a way to include scripts to the beforeRender so that they simply call our api instead of copy pasting code?</p>
]]></description><link>https://forum.jsreport.net/post/1150</link><guid isPermaLink="true">https://forum.jsreport.net/post/1150</guid><dc:creator><![CDATA[Oshri Cohen]]></dc:creator><pubDate>Tue, 26 Sep 2017 17:47:22 GMT</pubDate></item><item><title><![CDATA[Reply to Report call our api beforeRender for only one report on Invalid Date]]></title><description><![CDATA[<blockquote>
<p>Can a jsreport template call our api to pull data instead of having to push data to it?<br />
I understand that there is the beforeRender event that runs in nodeJS with all templates, how can I have it executed for only one template because the call will be different based on input parameters and context.</p>
</blockquote>
<p>yes, you can pull data in a jsreport script. scripts can run globally (for all templates) or just in the scope or just one template, in studio you can choose which scripts should run in the context of your template:</p>
<p><img src="/uploads/files/1506450637101-captura-de-pantalla-2017-09-26-a-las-1.29.41-p.m..png" alt="0_1506450634369_Captura de pantalla 2017-09-26 a las 1.29.41 p.m..png" class="img-responsive img-markdown" /></p>
<p>there is an example called <code>Orders</code> that is present in every jsreport installation, this example shows how to call a remote API to pull data to use in the jsreport pipeline.</p>
<p>example <code>Orders scripts</code> (if you are going to try it in your server make sure to allow the <code>http</code> module in your jsreport configuration):</p>
<pre><code class="language-js">// custom server side script used to fetch data from remote REST API
var http = require('http');

function getOrders(country, cb) {
    http.get({
        hostname: 'services.odata.org',
        port: 80,
        path: `/V4/Northwind/Northwind.svc/Orders?$filter=${encodeURI(`ShipCountry eq '${country}'`)}`,
    }, (result) =&gt; {
        var str = '';
        result.on('data', (b) =&gt; str += b);
        result.on('error', cb);
        result.on('end', () =&gt; cb(null, JSON.parse(str)));
    });
}

function beforeRender(req, res, done) {
    // the report parameter country can be send from the client API request
    req.data.country = req.data.country || 'France'
    getOrders(req.data.country, (err, json) =&gt; {        
        if (err) {
            return done(err);
        }
        
        var orders = json.value;
        var ordersByQuarter = {};
        
        orders.forEach((o) =&gt; {
            o.OrderDate = new Date(o.OrderDate);
            var key = o.OrderDate.getFullYear() + '/' + (o.OrderDate.getMonth() + 1);
            ordersByQuarter[key] = ordersByQuarter[key] || {
                value: 0,
                orderDate: o.OrderDate
            };
            ordersByQuarter[key].value++;
        });

        req.data.orders = orders;
        req.data.accumulatedOrders = ordersByQuarter;

        done();
    });
}
</code></pre>
<p>in this script you can see how we use the <code>getOrders</code> function inside the <code>beforeRender</code>, <code>getOrders</code> gets data from a remote API (using node.js <code>http</code> module) and set the returned data to <code>req.data.orders</code>, which will be used when rendering the template. you can do it in the same way for your case but taking into account any previous input/parameter that you provide, this will help you to prepare your request for the remote API that you want to consume.</p>
<blockquote>
<p>Also is there a way to include scripts to the beforeRender so that they simply call our api instead of copy pasting code?</p>
</blockquote>
<p>for example you can put the <code>getOrders</code> function in an <a href="https://jsreport.net/learn/assets" rel="nofollow">asset</a>, then in your script import the asset and use the function inside <code>beforeRender</code> function normally. that would help you to avoid repeating code.</p>
]]></description><link>https://forum.jsreport.net/post/1152</link><guid isPermaLink="true">https://forum.jsreport.net/post/1152</guid><dc:creator><![CDATA[bjrmatos]]></dc:creator><pubDate>Invalid Date</pubDate></item></channel></rss>