12

When I send an object with an array of objects in it from my express route to my client, I get an [Object object] and then when I try to stringify it, I get this crazy string with this console message

var messages = "<%=(JSON.stringify(messages))%>"
console.log(messages) 

Which prints this out to the console ...

{&#34;messages&#34;:[{&#34;content&#34;:&#34;cool mane&#34;,&#34;creator&#34;:&#34;joe&#34;},{&#34;content&#34;:&#34;test 4&#34;,&#34;creator&#34;:&#34;joe&#34;},{&#34;content&#34;:&#34; ewgdqf&#34;,&#34;creator&#34;:&#34;joe&#34;},

It should be something so I can iterate through it by doing messages[0].content but I'm getting this crazy string that won't let me do anything with it...

If I try to loop through it, it just prints out each character by itself.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
joe
  • 1,563
  • 4
  • 16
  • 35
  • 2
    Seems like `<%= ... %>` is HTML encoding the output. Template engines often provide a way to output stuff without encoding it. Once you fixed that, you need to `JSON.parse` the string as well. Still wondering whether there is a better way to pass the object to the client. – Felix Kling May 09 '16 at 16:37
  • i don't think there's any other way to send data to the client, but JSON.parse gives me an error everytime I try to use it even after I stringify it – joe May 09 '16 at 16:43
  • 1
    @T.J.Crowder that's just a typo from trying 100 different things I accidently kept them when I went back to my original code – joe May 09 '16 at 16:45
  • As you said, you need to fix the output before you can use `JSON.parse`. You have to use the non-encoding version of `<%= ... %>`, whatever that is. Which template engine are you using? – Felix Kling May 09 '16 at 16:45
  • According to http://stackoverflow.com/a/16184093/218196, you have to use `<%-`. So your code should be: `var messages = JSON.parse('<%-JSON.stringify(messages)%>');`. Give that a try... – Felix Kling May 09 '16 at 16:49
  • just pointing out that `console.dir()` is sometimes useful on complex objects – Mark Schultheiss May 09 '16 at 16:50
  • @FelixKling I've tried that but I keep getting the Uncaught SyntaxError: Unexpected identifier error when I use the - instead of = – joe May 09 '16 at 16:51
  • 2
    Uh, I'm stupid. Just do `var messages = <%-JSON.stringify(messages)%>;`. – Felix Kling May 09 '16 at 16:52
  • @FelixKling the single quotes were my problem, it's working now and the format is actually readable no more " But now it's still treating it like a giant string. I can't access the message array inside of it by doing messages.messages – joe May 09 '16 at 16:53
  • @FelixKling - NO not stupid, :) we all have those "can't see the obvious" moments. I let at my former co-worker Max look at mine while I explain to him what it does. He passed away several years ago but still helps me out often. – Mark Schultheiss May 09 '16 at 16:55
  • @FelixKling okay after doing messages = JSON.parse(messages) everything is working perfectly. Thanks for your help. – joe May 09 '16 at 16:57
  • @MarkSchultheiss I think felix was actually right.. I just messed up the syntax with using " instead of ' – joe May 09 '16 at 16:58
  • Yeah, that will work, but only until your data contains a `'` somewhere ;) See my comment and answer. – Felix Kling May 09 '16 at 16:58

2 Answers2

19

When using <%= ... %>, EJS will encode / escape any output. That's why the " in the JSON are encoded as &#34;. 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)%>;
Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
-1

Try to change this :

var messages = "<%=(JSON.stringify(messages))%>"
console.log(messages) 

With this :

var messages = JSON.stringify("<%=messages%>");
console.log(messages) 
Akram Saouri
  • 1,179
  • 8
  • 15