Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
95
rated 0 times [  98] [ 3]  / answers: 1 / hits: 78965  / 12 Years ago, sun, december 9, 2012, 12:00:00

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 = {&quot;__v&quot;:0,&quot;_id&quot;:&quot;50bc01938f164ee80b000001&quot;,&quot;agents&quot;:...


Is there a way to pass a JSON that will be JS readable?


More From » json

 Answers
5

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)


[#81522] Friday, December 7, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cristopherh

Total Points: 402
Total Questions: 117
Total Answers: 84

Location: Monaco
Member since Fri, Sep 24, 2021
3 Years ago
;