API not being called by request in beforeRender



  • Hell - im a newbe so please forgive my ignorance, but following all the examples i am trying to get API data as follows:

    var request = require('request');
    function  beforeRender(done){
        console.log('hello');
        request({
            url:"http://127.0.0.1:8000/api/receipts/1",
             method: "POST",
            json:true,
            headers: {'User-Agent':'Super Agent/0.0.1','content-type': 'application/json','Content-Length': 92},
          },function(err, response, body){
           
            console.log(body);
            request.data = {posts:body};
           console.log(request.data);
            done();
        })    
    }
    

    using the debug, i find that the script is being executed:

    +0      Starting rendering request 26 (user: null)
    +7      Rendering template { name: index, recipe: chrome-pdf, engine: handlebars, preview: true }
    +13     Data item not defined for this template.
    +19     Resources not defined for this template.
    +31     Executing script indexScript
    +1241   hello
    +1264   Base url not specified, skipping its injection.
    +1268   Rendering engine handlebars
    +1853   Compiled template not found in the cache, compiling
    +1873   Executing recipe chrome-pdf
    +2271   Converting with chrome HeadlessChrome/72.0.3617.0 using dedicated-process strategy
    +2802   Running chrome with params {"printBackground":true,"margin":{}}
    +3344   Skipping storing report.
    

    But my fiddler tells me the http request to http://127.0.0.1:8000/api/receipts/1 is never made for some reason...

    I'm banging my head on this one



  • Are you using the latest jsreport,or at least the v2 version?
    The script hooks have different signature there. See the docs
    https://jsreport.net/learn/scripts

    Your script should look like this

    var request = require('request');
    function  beforeRender(req, res, done){
        console.log('hello');
        request({
            url:"http://127.0.0.1:8000/api/receipts/1",
             method: "POST",
            json:true,
            headers: {'User-Agent':'Super Agent/0.0.1','content-type': 'application/json','Content-Length': 92},
          },function(err, response, body){
           
            console.log(body);
            req.data = {posts:body};
           console.log(req.data);
            done();
        })    
    }
    

Log in to reply
 

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