How to get object properties of a collection


#1

Hi,

Snipped some code, but below should give idea what I’ve done.

Defined the seed data.

   var mylist = [{
         num: "ABC",
         items: [{  
            line: 1,
            item: "001"
         }, {
            line: 2,
            item: "002"
         }]
      }];

Created the template.

<template name="orders">      
  {{#each orders}}
     {{num}}
  {{/each}}
</template>

Created the helper.

Template.orders.helpers({
   orders ( ) {
      return orders.find({});
   }
});

Order numbers are being listed just fine. Now my problem, I want to show the items.

<template name="items">
  {{#each items}}
    {{item}}
  {{/each}}
</template>

Here’s the helper

Template.items.helpers ({
  items ( ) {

// my question below.

     }
});

how to return the items of the order?

TIA!


#2

You would use the same Query to get the full collection, then cycle through each order, and then through the Items in the order.

{{#each orders}}
    {{num}}
    {{#each items}}
        {{item}}
    {{/each}}
{{/each}}

This is how I handle it with sub-documents that I iterate through.


#3

ouch. that simple? :slight_smile:

thank you. i thought i tried that already, and it didn’t work. maybe some typo.

follow up question if i may, how do you find a record in the sub document?


#4

So, you can do something like

Collection.find({ "order.item": <item number> })

I also find that the innerloop {{#each}} for sub-documents works better if I create the inside loop in it’s own template, then iterate over the template.