Mising helpers 'toString'



  • Hello,

    I'm just starting with jsreport (also with JS language in fact...) and I've got this error when running my document and I don't know what could be wrong:

    Error while executing templating engine. Missing helper: "toString"
    Error: Missing helper: "toString"
    at Object.<anonymous> (C:\Users\jsreportapp\node_modules\handlebars\dist\cjs\handlebars\helpers\helper-missing.js:19:13)
    at Object.wrapper (C:\Users\jsreportapp\node_modules\handlebars\dist\cjs\handlebars\internal\wrapHelper.js:15:19)
    at Object.eval [as main] (eval at createFunctionContext (C:\Users\jsreportapp\node_modules\handlebars\dist\cjs\handlebars\compiler\javascript-compiler.js:262:23), <anonymous>:21:110)
    at main (C:\Users\jsreportapp\node_modules\handlebars\dist\cjs\handlebars\runtime.js:208:32)
    at ret (C:\Users\jsreportapp\node_modules\handlebars\dist\cjs\handlebars\runtime.js:212:12)
    at ret (C:\Users\jsreportapp\node_modules\handlebars\dist\cjs\handlebars\compiler\compiler.js:519:21)
    at C:\Users\jsreportapp\node_modules\jsreport-handlebars\lib\handlebarsEngine.js:33:20
    at Object.base.apply (C:\Users\jsreportapp\node_modules\vm2\lib/contextify.js:469:32)
    at evaluate-template-engine.js:1:64
    at Script.runInContext (vm.js:143:18)

    Let me know if you need me to paste my code.

    Thanks!


  • administrators

    hi! yes it will be better if your paste you code, but the issue should be caused by not defining a toString function in the helpers section of the template, such helper is not part of the default handlebars helpers



  • I'm no sure what should I add on my global helpers.js file...Could you please let me know?
    Here's my script code:

    const request = require('request');
    
    async function getMaApi() {
        const query = `{
      monitorData(machineId:"8af00d441c99a48aba6666a4"){
        trendValues(variables:["DT:addr_100","DT:addr_118","DT:addr_120"]since:1606842060000,until:1606842643000){
          label
          values{
            timestamp
            value
          }
          
        }
      }
    }`;
       var postData = JSON.stringify({
                query,
                variables: null,
            })
    
        const options = {
            url: 'https://ecostruxure-machine-advisor.se.app/daas/graphql',
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Authorization': 'Bearer 9RfMdTD8Nh3aMd8xN9cRSr5SKwTV6fCTpyQcQcA5'
                },
            body: postData
        };
        function doRequest(options){
            return new Promise(function(resolve,reject){
                request(options, function(err,res,body){
                    resolve(body)
                })
            })
        }
        let response = await doRequest(options) //espera hasta que la promesa se resuelva
        //return JSON.parse(response).data.me.username - Ejemplo para acceder a un campo de la respuesta
    
        let trendValues = JSON.parse(response).data.monitorData.trendValues;
    
        for (let tagIndex = 0 ; tagIndex < trendValues.length; tagIndex++){
            for (let valueIndex = 0; valueIndex < trendValues[tagIndex].values.length; valueIndex ++){
                let timestampValue = trendValues[tagIndex].values[valueIndex].timestamp
                trendValues[tagIndex].values[valueIndex].timestamp = new Date(timestampValue*1000);
            }
        }
        let formattedResponse = trendValues;
        console.log(formattedResponse)
        return formattedResponse;
    }
    
    //Antes de renderizar hay que hacer algo. función JSReport
    async function beforeRender(req, res) {
        req.data.trendValues = await getMaApi(); //en req.data añadimos toda la info que usaremos en el report. En machines
        //hemos asociado toda la respuesta
    }
    const nodemailer = require('nodemailer');
    async function afterRender(req, res) {
        const transporter = nodemailer.createTransport({
          host: 'smtp.office365.com',
          port: 587,
          auth: {
              user: 'mnogueracolome@outlook.com',
              pass: '*****'
          }
        })  
    
        await transporter.sendMail({
          from: "mnogueracolome@outlook.com",
          to: "mnogueracolome@gmail.com",
          subject: "Sending email from node.js",
          text: "See the attached report",
          html: "<b>See the attached report</b>",
          /*attachments: [
          {  
            filename: 'Report.pdf',
            content: Buffer.from(res.content)
          }],*/
        })       
    }
    

  • administrators

    @Akeold hmm i'm a bit confused, do you have a global helpers.js file and this is the code that is inside it? if yes, then it is probably wrong because you are trying to define a script inside a file that is supposed to contain just helpers.

    i think to understand better the shape of your template and entities you will need to share an export zip, so i can understand what things are wrong and where you need to define the toString helper



  • Sorry @bjrmatos , how can I attach a file here? do I have to post a downloading link?


  • administrators

    @Akeold click the "Reply button" here

    0_1610120804868_Captura de pantalla 2021-01-08 a la(s) 10.46.20 a. m..png

    then just drag and drop the file in the "compose" textbox (the one on the left)

    0_1610120864145_Captura de pantalla 2021-01-08 a la(s) 10.46.59 a. m..png



  • This post is deleted!


  • seems that I don't have enough privileges:

    0_1610266290965_upload-c9d5ab39-41a5-4af5-8acd-6e8ed774bb56



  • Any idea regarding this error?


  • administrators

    that was probably a problem in the server, do you get the same if you continue trying it multiple times?



  • Yes... But regarding my initial issue, I've found the problem.
    If added this function down to the template (html one) and then everything ok.

    function toString (data) {
    return JSON.stringify(data);
    }

    thanks for your support anyway.


Log in to reply
 

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