I have codes in EJS below,
<script>
var row =<%-JSON.stringify(data)%>
console.log(row);
</script>
<% for(var i=0; i<JSON.stringify(data).length; i++) {%>
<tr>
<td>
<%= JSON.stringify(data)[i].id%>
</td>
</tr>
<% } %>
output of row is correct, an array of 3 objects, each with properties id, name etc.. I can manipulate the row to popuate the table in JS. However, I am wonderring whether there is a way to allow it be done in the above manner?
When I run the code above, JSON.stringify(data).length is not 3, but rather the length of the whole string.
Another questions is when I try to add
<% alert('t'); %> or <% window.alert('t'); %>, it gives me 'not defined' error...
Helps appreciated.
Regards Hammer
data
since it is being manipulated on the server (i.e. you want to manipulate it as a real object, not as a string). Regarding your second question,alert
andwindow
are browser-specific and aren't n node. – Barling