I'm passing two variables to my front end like this
res.render("mypage", {data: data});
Using the data within my variable, I want to build some HTML elements and do some data visualization using charts. I can of course just use my templating software (EJS) and write a whole lot of javascript within the HTML file itself, but that doesn't sound sensible.
I searched around stackoverflow and the general solution seems to be to put it into a global variable and then load in the external js file so it has access to it, so on my front end I would have this
<head>
<title>Cool Site</title>
<script>
var data = <%- data %>;
</script>
<script src="/fun.js"></script>
</head>
The problem is that VSCode itself says Expression Expected for my <%- and %> parts, and when I try to access the variable within my fun.js file, it says Data is not defined and also says SyntaxError: Unexpected token.
Any ideas on how I can properly access the data?
<%- JSON.stringify(data) %>work? Otherwise, your script tag may contain something likevar data = [Object object];.