jsrender helpers template - convert int variable to string

  • Hello, I am passing some data into my template and some of the data are long integers which i want to convert into strings.

    I read through the following documentation - https://jsreport.net/learn/handlebars

    I followed the helper example where you convert a string to uppercase, that works perfectly. I created a new helper -

    function convetToString(str) {
        return str.toString();

    and in the template wrote the following {{{convetToString 100000}}}

    Problem 1: I want the output to be 100,000 but instead it just outputs 100000.

    Problem 2: once problem 1 is fixed instead of {{{convetToString 100000}}} i want to put {{{convetToString data.number}}} but that throws the following error

    Error while executing templating engine. Cannot read property 'toString' of undefined. Error on line 24:16.
      22 | 
      23 | function convetToString(str) {
    > 24 |     return str.toString();
         |                ^
      25 | }
      26 | 
      27 | function toUpperCase(str) {

    I have also tried this using {{{toUpperCase name}}} (name is one of the values i passed through in the data) and i get the same error even though that function works if i put {{{toUpperCase "Luke Rayner"}}}

  • Please try to share the whole example in playground
    Your data.number doesn't exists likely.

    For formatting numbers, you probably want to use a library like numeraljs

Log in to reply

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