0

My problem is that when i send data to handlebars template it converts my array of strings to array of variables:

res.render('index', { 
  title: 'Express' ,
  chartT:"title",
  lab:['test','test2','test3'],
  dat:[1,3,5]
});

Template code:

  <canvas id="myChart" width="400" height="400"></canvas>
    <script>
      var pollOptions = [{{{lab}}}];
      var pollData = [{{{dat}}}];
      createChart("mychart",pollOptions,pollData);
    </script>

And the problem that in pollOptions i got this

var pollOptions = [test,test2,test3];

Uncaught ReferenceError: test is not defined

I don't know why the template convert them...

Thank you in advance for your help.

2 Answers 2

1

HandleBars dedicated to HTML templating (Not to render a JS variable) You can do something like this (Without handlebars):

var pollOptions = ["{{{lab}}}"];
var pollData = ["{{{dat}}}"];
Sign up to request clarification or add additional context in comments.

5 Comments

I use handlebars for templating...e.g.:<h1>{{title}}</h1> passing down data from the server so it can show to the user, but i also need to supply data for the chart (the variables there to make the code more readable)
Yes, i know. What caused the caught error is that handlebars removes ' from the variables which means javascript engine will consider them as javascript variable instead of String you're expecting.
Ok, but what is the solution , can i force down the strings or should i make an ajax call to the server ( to a different route) that will supply the data to the chart or register a helper or just get rid of the template engine and use some front-end framework or libraries? The solution above doesn't work even with html.
unfortunately the updated solution doesn't work the result is: ["test,test,test"]
I ended up creating a new route(/api/getPools) and got the data with fetch, thank you for trying to help, i am going to accept your solutions for clarifying that i cannot send down strings
0

Ok I have figured it out!

var rawData = "{{{lab}}}";
var pollData = rawData.split(",");

And now i can use it as a string array

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.