Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I don't know about db_objects, when I try and access that property on a cursor (which is what <code>find()</code> returns), it's <code>null</code>.</p> <p>You could fetch the items that matches your query instead, and then do your iteration:</p> <pre><code>Template.content.categories_parents = function (){ var parents = Categories.find({idCategoryParent:0}).fetch(); // Returns an array. for (var i = 0; i &lt; parents.length; i++){ var pId = parents[i]['idCategory']; var children = Categories.find({idCategoryParent:pId}); // No need for array here, cursor is fine. parents.db_objects[i]['children'] = children; } return parents; } </code></pre> <p>I'm new at this myself, so maybe there's a more efficient way of doing it, but I don't know it currently.</p> <p><strong>Update after Eric's comment.</strong></p> <p><strong>The js file looks like this:</strong></p> <pre><code>Categories = new Meteor.Collection("categories"); if (Meteor.isClient) { Template.categories.categories = function () { var parents = Categories.find({idCategoryParent:0}).fetch(); for (var i = 0; i &lt; parents.length; i += 1) { var pId = parents[i]['idCategory']; var children = Categories.find({idCategoryParent:pId}); parents[i]['children'] = children; } return parents; }; } if (Meteor.isServer) { Meteor.startup(function () { Categories.remove({}); var data = [ {idCategoryParent: 0, idCategory: 1, label: "Movies"}, {idCategoryParent: 1, idCategory: 0, label: "Science Fiction"}, {idCategoryParent: 1, idCategory: 0, label: "Drama"}, {idCategoryParent: 0, idCategory: 2, label: "Music"}, {idCategoryParent: 2, idCategory: 0, label: "Jazz"}, {idCategoryParent: 2, idCategory: 0, label: "Piano"} ]; for (var i = 0; i &lt; data.length; i += 1) { Categories.insert(data[i]); } }); } </code></pre> <p><strong>The html file looks like this:</strong></p> <pre><code>&lt;head&gt; &lt;title&gt;nested_template&lt;/title&gt; &lt;/head&gt; &lt;body&gt; {{&gt; categories}} &lt;/body&gt; &lt;template name="categories"&gt; &lt;h1&gt;Categories&lt;/h1&gt; {{#each categories}} &lt;b&gt;{{label}}&lt;/b&gt; &lt;ul&gt; {{#each children}} &lt;li&gt;{{label}}&lt;/li&gt; {{/each}} &lt;/ul&gt; {{/each}} &lt;/template&gt; </code></pre> <p>It works just fine for me.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload