Parameterization of variables in beforeRender and afterRender script



  • It is possible to define and send parameters from the template to the scripts that have functions berofeRender and afterRender, to send a parameter get, or path variable, and obtain different results, and thus in this way reuse the code in different templates by consulting the same api , and only change the value of the parameter in the template.

    var getRequest = require('request').get
    
    function beforeRender(req, res, done) {
        getRequest('http://misitioweb/v1/data/user/', function (err, resp, body) {
            if (err) {
                return done(err)
            }
            
            req.data.user = JSON.parse(body).data
            done()
        }) 
    }


  • You can send the parametr in the API call and then reach it for example on req.options or req.data.
    This is typical scenario.

    I am not sure I understand what you mean with "sending parameter from the template".



  • The idea is to reuse a rest service, for 'n' template, but for this it is necessary to receive a parameter, in rest services I can obtain data from a certain user, and thus fill in the corresponding data in each template, each template represents a user and this pdf template, let's say a report is sent by email every client every so often through a shedule. In this way avoid generating a static template per client, but cunsumir the information of a rest service, just changing a parameter.


Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.