I want to use variable (or variables) in ejs file in the script tag ,I also want to pass the rendered file to a function for taking screenshot from ejs file.
But now I have 2 problems :
1.I don't know how to pass the variable in server file to the ejs file and render and use it without app.get... (in express) because it's server side and I want to use html file.
2.I don't know how to use variable in ejs file in the script tag
these are my files :
index.ejs
<div id="tvchart"><% symbol %></div>
<script>
//some codes
var symbolD =<%= symbol %>;
fetch(`http://127.0.0.1:9665/fetchAPI?endpoint=https://api.binance.com/api/v3/klines?symbol=${symbolD}&interval=1m&limit=1000`)
</script>
server.js
// set the view engine to ejs
app.set("view engine", "ejs");
const symbol = "EGLDUSDT"
const file = ejs.render("./index.ejs" , symbol);
console.log(file)
So Why my ejs and server file doesn't work?
The second argument to render needs to be an object where the property names are the variables passed to the EJS file.
ejs.render("./index.ejs" , {symbol});
Once you fix that var symbolD =<%= symbol %>; gives you var symbolD =EGLDUSDT; which throws an error because you don't have a variable named EGLDUSDT.
Use JSON.stringify to generate something compatible with JavaScript literal syntax (i.e. with quotes around strings and proper escaping for special characters).
var symbolD = <%= JSON.stringify(symbol) %>;