How to pass a array from node to .js file (pugJs)

前端 未结 1 1268
说谎
说谎 2021-01-25 18:30

I am trying to render a chart (using ChartJS) as express response. The data points for the chart are stored in node as array. I have to pass it on to the chartJS fu

相关标签:
1条回答
  • 2021-01-25 19:12

    You need to use unescaped string interpolation and a stringify to turn your pug variable into a client-side browser variable:

    var arrayForChart = !{JSON.stringify(arrayInPug)};
    

    If the variable in express/pug is ['a', 'b', 'c'] then this will output:

    var arrayForChart = ['a', 'b', 'c'];
    

    Without the stringify you'll get something like this which will just throw an error in the browser as it's not valid JavaScript:

    var arrayForChart = [Object object];
    
    0 讨论(0)
提交回复
热议问题