Input SQL results to report



  • Hi JS-Team,

    Here is the code that i'm struggle with.

    GetreportSchedule : function(req, res){
                   var query = db.query("Select * from trainStops", function (err, result) {
                    if (err) {
                        console.log(req.body);
                     } else {
                     var objs = [];
                     for (var i = 0;i < result.length; i++) {
                            objs.push({"id": result[i].s_Id,"name":result[i].name,"arr":result[i].arr,"dept":result[i].dept});
                      }
                
                       var arrayX = JSON.stringify(objs);
                
                        var data = {
                              template:{"shortid":'BynN_cl0l'},
                              data:{"trainStops":arrayX}
                         }
                         var options = {
                               uri:'http://localhost:8001/api/report',
                               method:'POST',
                               json:data
                         }
                         request(options).pipe(res);  
            }
        });
    },
    

    When i replace arrayX with its output, pdf works fine. But when i use arrayX (as above example) i'll get an empty pdf.
    Can someone please tell me what I did wrong?



  • Does this work with html recipe? ... I don't understand why you call JSON.stringify(objs); when all other values are passed as json objects.



  • Also, you should probably use the json:true, body: data in options https://github.com/request/request



  • Thanks for the reply sir!

    i call JSON.stringify(objs) to create the json object as what i needed according to the report. And Yes! report is working fine in html recipe.
    However "json:true, body: data" didn't work out.

    for more clarity:

    when i wrote below line manually report working perfectly.
    data: {"trainStops":[{"id":"3007","name":"Colombo Fort","arr":"07:05","dept":"07:06"},{"id":"3008","name":"Airport","arr":"07:00","dept":"07:01"}] }

    console.log(arrayX) gives below output:
    [{"id":"3007","name":"Colombo Fort","arr":"07:05","dept":"07:06"},{"id":"3008","name":"Airport","arr":"07:00","dept":"07:01"}]

    but when i'm using below code report shows as an empty page.
    data: {"trainStops": arrayX }

    My English is not so good, sorry for that and hope you understood my problem anyway. :D


Log in to reply
 

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