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.

5
  • Based on that other question you referenced, it is not clear what you have in your locals variable. Commented Nov 1, 2016 at 4: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<locals.teams.length;i++){ %> <ul><li> Name:<%= locals.teams[i]._source.name %> </li></ul> <%}}%> Commented Nov 1, 2016 at 4:33
  • Ok, I got that part, but how does your array look like when your code fails? Commented Nov 1, 2016 at 4: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. Commented Nov 1, 2016 at 4: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<locals.players[i][j].length;j++){..}. Now I don't have errors. But no display of players array data. Working on it now.. Commented Nov 2, 2016 at 1:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.