When I JSON.stringify(object) I get a crazy string as a value
When using <%= ... %>
, EJS will encode / escape any output. That's why the "
in the JSON are encoded as "
. According to this answer, you can prevent escaping by using <%- ... %>
instead.
There is also no need to put the output inside a string literal. It's actually bad since you can get problems with nested quotes. Just let it output directly into the JS code:
var messages = <%-JSON.stringify(messages)%>;
Try to change this :
var messages = "<%=(JSON.stringify(messages))%>"console.log(messages)
With this :
var messages = JSON.stringify("<%=messages%>");console.log(messages)