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.



  • I have same question.

    The example for sending a report via email uses a hard-coded recipient.

    In the beforeRender section, the req variable contains whatever data is needed to generate the report.

    I would just like to include the recipient email address in that req variable so that the afterRender function can send the message to the desired recipient.


  • administrators

    In the beforeRender section, the req variable contains whatever data is needed to generate the report.
    I would just like to include the recipient email address in that req variable so that the afterRender function can send the message to the desired recipient.

    hmm i think you can just set that email in the beforeRender script, then you can use it normally in the afterRender script.

    async function beforeRender (req, res) {
      // let't say you get data from another server here
      const fromServer = await fetch(...)
      req.data.payload = fromServer.data
      req.data.recipientEmail = fromServer.recipientEmail
    }
    
    async function afterRender (req, res) {
      // the code to send email here just need to read req.data.recipientEmail
    }
    

    the above should work, is not working? i'm not sure why the above won't be enough to stop relaying on hard-coded value for the email. if i'm missing something let me know.



  • What you say sure makes sense and what I am doing. However, the value of req is different between the two calls.

    As I was typing the above line, I realize that for some reason, I was modifying the req.data field in my beforeRender call, which screwed up the value.

    Fixed that and all is working as it should, my bad :(


  • administrators

    haha yes, sometimes it is easy to miss that, great that it is working now 👏👏👏


Log in to reply
 

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