In my index.ejs I have this code:
var current_user = <%= user %>
In my node I have
app.get("/", function(req, res){ res.locals.user = req.user res.render("index") })
However, on the page I obtain
var current_user = [object Object]
and if I write
var current_user = <%= JSON.stringify(user) %>
I obtain:
var current_user = {"__v":0,"_id":"50bc01938f164ee80b000001","agents":...
Is there a way to pass a JSON that will be JS readable?
Advertisement
Answer
Oh that was easy, don’t use <%=
, use <%-
instead. For example:
<%- JSON.stringify(user) %>
The first one will render in HTML, the second one will render variables (as they are, eval)