Chartjs-Cannot access a variable declared outside a For loop (Javascript)

0👍

the {% for item in string %} does not look like javascript. is that some ruby thing or something? Its looking for the the variable string defined in whatever non-javascript language that tag is coming from, and its undefined or uninitialized since it doesn’t have access to any javscript variables.

If you make it a JS array (instead of an array declaration inside of a string, as it currently is), You can just assign the js array directly without even looping, ie

var string = ['text1', 'text2', 'text3', 'text4']; //no double quotes on the outside
var barData = {
labels : string,
//...

The reason your data loop does work is because the variable, an array literal, is defined inside of the tag, so it does have access to it.

Leave a comment