Query parameter in JSReports 2.0.0

  • I am trying to utilize a query parameter to modify a SQL query in my template. For the purposes of this example I would be passing in a LIMIT variable to insert into the SQL. Can someone help me spot where my mistake is? Thanks in advance

    var pg = require('pg');
    var conString = "postgres://user:password@conn/database";
    var client = new pg.Client(conString);
    function beforeRender(req, res, done) {
        client.connect().then(async function() {
        // You would call it using the URL 
        const paramA= req.context.http.query.paramA
        req.data.paramA = paramA
        var s = `SELECT * FROM schema.table WHERE state = 'California' LIMIT ${paramA} ;`;
            var q1 = await client.query(s)
                .then(function(recordset) {
                    console.log("recordset: ", recordset);
                    Object.assign(req.data, { fires: recordset.rows });

  • How is the request to render template initiated?

    Remove everything except

    function beforeRender(req, res) {

    What does it print if your run it with debug button?

  • I updated it to:

    function beforeRender(req, res) {
    console.log("PARAMS: ", req.context.http.query)

    and get an empty JSON
    +0 Starting rendering request 81 (user: null)
    +4 Rendering template { name: cw-test, recipe: chrome-pdf, engine: handlebars, preview: true }
    +5 Data item not defined for this template.
    +5 Resources not defined for this template.
    +12 Executing script loadtest
    +16 PARAMS: {}
    +83 Base url not specified, skipping its injection.
    +83 Rendering engine handlebars
    +97 Taking compiled template from engine cache
    +99 Executing recipe chrome-pdf
    +897 Running chrome with params {"printBackground":true,"margin":{}}
    +983 Skipping storing report.
    +1017 Rendering request 81 finished in 1017 ms

  • Ah. My mistake...

    I guess you initiate the rendering by using the url provided after you click the "link" button in the toolbar right?


    Afterwards you just add to url ?paramA=xxx and open the link in the next tab?

    change the script to

    function beforeRender(req, res) {
      req.template.content = JSON.stringify(req.context.http.query)

    And open the link again. I believe you see my point.

  • You are correct! That worked

    function beforeRender(req, res) {
    req.template.content = JSON.stringify(req.context.http.query)
    console.log("PARAMS34: ", req.template.content)

    with the URL http://localhost:5488/templates/Syb1dp8FQ?limit=1

    Let me work with this for few. Thank you!

  • with the help of one of our developers, I got it to work perfectly. Instead of stringify, we used the array and looped through it looking for the particular parameter. I'm sure there is more elegant ways of accomplishing it but this is a great start.
    Thanks for all your help!

    paramArray = Object.keys(req.context.http.query)
    for (i=0; i < paramArray.length; i++){
        var key = paramArray[i]
        if(key === "firstparam"){
            FirstParam = req.context.http.query[key]
        } else if (key === "secondparam"){
            SecondParam = req.context.http.query[key]

Log in to reply

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