0

I am sending a request to create pdf in node.js. The dataVal is dynamic.

app.get("/RequestForPDF", async function (req, res) {
  res.render("pdftemp1", {
    dataVal: mydataArray,
    author: "John Doe",
  });
});

"mydataArray" is a list of int dataset. I want to create a bar chart using that data by chart.js in the client side but I am unable to access the dataset. Can you suggest me a way to get through this? This is the script tag of my pdftemp1.hbs file.

<script>

var ctx = document.getElementById("myChart").getContext("2d");
var chart = new Chart(ctx, {
  
  type: "bar",

  // The data for our dataset
  data: {
    labels: [
      "Jun 2016",
      "Jul 2016",
      "Aug 2016",
      "Sep 2016",
      "Oct 2016",
      "Nov 2016",
      "Dec 2016",
      "Jan 2017",
      "Feb 2017",
      "Mar 2017",
      "Apr 2017",
      "May 2017",
    ],
    // Information about the dataset
    datasets: [
      {
        label: "Rainfall",

        borderColor: "royalblue",
        data: {{ dataVal }}, // Here I am using the array dataVal
        backgroundColor: "green",
      },
    ],
  },

  // Configuration options
  options: {
    layout: {
      padding: 10,
    },
    legend: {
      position: "bottom",
    },
    title: {
      display: true,
      text: "Precipitation in Toronto",
    },
    scales: {
      yAxes: [
        {
          scaleLabel: {
            display: true,
            labelString: "Precipitation in mm",
          },
        },
      ],
      xAxes: [
        {
          scaleLabel: {
            display: true,
            labelString: "Month of the Year",
          },
        },
      ],
    },
  },
});

</script>
James Z
  • 12,104
  • 10
  • 27
  • 43
  • What error do you get? Your code looks right but `dataVal` may have a different structure. To be precise `dataVal` should contain a string such that if you replace it exactly at the handlebars it will produce valid JS code. Try sending `dataVal: [1, 2, 3]` to see if you get any results. – adelriosantiago Dec 14 '21 at 18:09

0 Answers0