0

I am having this Code the problem is that the line which has <%= propOfProp%> does't print anything while if I replaced it with console.log(propOfProp), it will work

<%  for (var x in data){ %>
    <%  if(data.hasOwnProperty(x)){ %>
    <%  var obj = data[x]; %>
    <%  for( var prop in obj){ %>                           
    <%  if(obj.hasOwnProperty(prop)){ %>
    <%  if(isNaN(prop)){ %>                                             
    <%  findName([prop],function(err,data){ %>
    <%  for(var h in data[0]) {%>       
    <%  var obj2 = data[0][h];  %>      
    <%  for (propOfProp in obj2) { %>
    <%  if(isNaN(propOfProp)){ %>   
    <%= propOfProp%>
    <% } }%>
    <% } %>
    <%  }); %>

    <%  } %>
    <%  } %>
    <%  } %>
    <%  } } %>
Smern
  • 18,746
  • 21
  • 72
  • 90
koooko
  • 557
  • 1
  • 4
  • 5
  • 1
    No offense but that is one ugly looking code. Where are you using console.log? – Rey Libutan May 27 '15 at 00:49
  • I mean replacing it with console.log will work. And if you have idea how to use ejs to make the code look cleaner please tell me. – koooko May 27 '15 at 00:53
  • I have to wonder why you are seeing if it has hasOwnProperty, once you're already looping through the properties? – Daemedeor May 27 '15 at 01:45
  • what do you mean replacing it with console.log(propOfProp) works? do you mean if you did <% console.log(propOfProp) %> works? IMO, since you are just using javascript there, you should just make it javascript. or do all of it on the server instead of trying to do it all right there and then bind it to the template, in general, the View is to render variables and not do complicated JS ... – Daemedeor May 27 '15 at 01:49

1 Answers1

0

What <%= %> in ejs means is to interpolate and escape it (see https://github.com/tj/ejs) and not just print out a js var, you may want to do <%- %> (see Render a variable as HTML in EJS)...

so a fix would be

<%  for (var x in data){ %>
    <%  if(data.hasOwnProperty(x)){ %>
    <%  var obj = data[x]; %>
    <%  for( var prop in obj){ %>                           
    <%  if(obj.hasOwnProperty(prop)){ %>
    <%  if(isNaN(prop)){ %>                                             
    <%  findName([prop],function(err,data){ %>
    <%  for(var h in data[0]) {%>       
    <%  var obj2 = data[0][h];  %>      
    <%  for (propOfProp in obj2) { %>
    <%  if(isNaN(propOfProp)){ %>   
    <%- propOfProp%>
    <% } }%>
    <% } %>
    <%  }); %>

    <%  } %>
    <%  } %>
    <%  } %>
    <%  } } %>

However, in the near future you may want to rethink doing all that code in the front end, since you end up calling the server either way . The idea of EJS is to render the code on the server and have all the data already there, and then you can loop through it. If possible put it into tags on the bottom or do it on the controller (if this is MVC). Also, you don't need to loop through the properties and then hasOwnProperties, it'll only loop through the properties already present.

Community
  • 1
  • 1
Daemedeor
  • 1,013
  • 10
  • 22