Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to render nested collections in Meteor?
    primarykey
    data
    text
    <p><strong>Summary:</strong><br /> Child categories nested inside of Parent Categories are not getting rendered in a Meteor template.</p> <p><strong>Details:</strong><br /> Consider a data model for 'Category' as such:</p> <pre><code>// Model Schema Category { idCategory : 20, (id of the category itself) idCategoryParent : 0, (idCategory of our parent category) defaultLabel : "Movies" (our label) } </code></pre> <p>There are parent categories and child categories. Parent categories have an idCategoryParent property value of 0. Child categories store the idCategory of their parents as their idCategoryParent property. I'm trying to loop through a collection of these Categories and render them in the following way: </p> <pre><code>&lt;b&gt;Movies&lt;/b&gt; // parent category is in bold &lt;ul&gt; // child categories are rendered as an unordered list &lt;li&gt;Horror&lt;/li&gt; &lt;li&gt;Comedy&lt;/li&gt; &lt;li&gt;Action&lt;/li&gt; &lt;li&gt;Drama&lt;/li&gt; &lt;/ul&gt; &lt;b&gt;Music&lt;/b&gt; &lt;ul&gt; &lt;li&gt;Rock&lt;/li&gt; &lt;li&gt;Classical&lt;/li&gt; &lt;li&gt;Ambient&lt;/li&gt; &lt;/ul&gt; </code></pre> <p>However, this is what I actually get:</p> <pre><code>&lt;b&gt;Movies&lt;/b&gt; &lt;ul&gt; // empty... &lt;/ul&gt; &lt;b&gt;Music&lt;/b&gt; &lt;ul&gt; &lt;/ul&gt; </code></pre> <p><strong>Source Code:</strong></p> <pre><code>// How we get the 'categories_parents' data Template.content.categories_parents = function (){ /* * Get all parent categories (categories with an idCategoryParent of 0) */ var parents = Categories.find({idCategoryParent:0}); var pCount = parents.count(); for (var i = 0; i &lt; pCount; i++){ var pId = parents.db_objects[i]['idCategory']; /* * Get all child categories of the parent (categories with * an idCategoryParent equal to value of parent category's idCategory). */ var children = Categories.find({idCategoryParent:pId}); var cCount = children.count(); /* * Assign the child categories array as a property of the parent category * so that we can access it easily in the template #each expression */ parents.db_objects[i]['children'] = children; } return parents; } // This is our template &lt;template name="content"&gt; &lt;h1&gt;Categories&lt;/h1&gt; {{#each categories_parents}} &lt;b&gt;{{defaultLabel}}&lt;/b&gt;&lt;br /&gt; &lt;ul&gt; {{#each children}} &lt;li&gt;{{defaultLabel}}&lt;/li&gt; {{/each}} &lt;/ul&gt; {{/each}} &lt;/template&gt; </code></pre> <p><strong>Other template configurations I have tried in troubleshooting:</strong></p> <pre><code>{{#each children}} &lt;li&gt;A Child Exists Here&lt;/li&gt; // Even this never rendered... no children? {{/each}} </code></pre> <p>Any clues as to why this is happening would be appreciated. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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