0

This is related to the a question I have asked previously. I have corrected my route according the answer give by @Val. With that I have an array containing arrays which has JSON objects. Ex:

[
  [{_index:'myIndex',_type:'myType',_source:{name:'Name',age:25}}, 
   {_index:'myIndex',_type:'myType',_source:{name:'Name',age:25}}
  ],
  [{_index:'myIndex',_type:'myType',_source:{name:'Name',age:25}},
   {_index:'myIndex',_type:'myType',_source:{name:'Name',age:25}}
  ]
]

So in my ejs file I am iterating through array as follows.

<% if(locals.players){ %>
<% for(var i=0;locals.players.length;i++){ %>
    //some more code
    <% var tp = locals.players[i]; %>
         <% for(var j=0;tp.length;j++){ %>
            <ul>
                <li>
                    Name:<%= tp[j]._source.name %><br>
                    Age:<%= tp[j]._source.age %>
                </li>
            </ul>
    <% } %>
<% } } %>

But I receive an error 'property _source undefined'. Just to add more details: the results array is coming from elasticsearch query. My intention is to print more results of another query which has objects from 1-D array. Second array contains arrays of results which correspond to each object in 1-D array.

Community
  • 1
  • 1
akalanka
  • 553
  • 7
  • 21
  • Based on that other question you referenced, it is not clear what you have in your `locals` variable. – Val Nov 01 '16 at 04:27
  • I render the index page in renderResults and the index.ejs contains the another ejs which is given above. There are two results arrays in renderResults: teams and players. To access those arrays we can use syntax like `<% if(locals.teams){%> <% for(var i=0;i
    • Name:<%= locals.teams[i]._source.name %>
    <%}}%>`
    – akalanka Nov 01 '16 at 04:33
  • Ok, I got that part, but how does your array look like when your code fails? – Val Nov 01 '16 at 04:36
  • The array that I have posted with my question is similar to that. When the array just have JSON objects like `[{_index:myIndex,_type:myType,_source:{a:a, b:b}},{_index:myIndex,_type:myType,_source:{a:a, b:b}},{_index:myIndex,_type:myType,_source:{a:a, b:b}}]` I can access to them `<%= locals.teams[i]._source.a%>` in a loop. – akalanka Nov 01 '16 at 04:58
  • I found a error in assigning nested array to `var tp`. Instead of that I changed the loop as `for(var j=0;j – akalanka Nov 02 '16 at 01:41

0 Answers0