Listing out array items from basic meteor mongo collection query


#1

I have my data returning, and I’m able to list the main data out in my template. I’m putting the data into a table, also working, except for an element in my document that is an array, and can have multiple key: value sets under it within the document.

I’m not sure how to get these to display in my template.

My document looks like this.

"_id": "qL3mo7JSYsobmRoy7",
  "bastasId": "1",
   "route": "1",
  "name": {
    "first": "Shirley",
    "last": "Maxwell"
  },
  "gender": "F",
  "address": {
    "streetAddress": "121 Margot Ln",
    "complexName": "Millbrook",
    "aptNo": "21A",
    "city": "Lubbock",
    "state": "TX",
    "zip": "79501"
  },
  "phone": {
    "home": "806-232-1432",
    "cell": "806-345-1234"
  },
  "notes": "Notes on Morgot Ln",
  "gifts": [
    {
      "giftType": "Shoes",
      "giftSize": "M",
      "selected": "false",
      "checkedIn": "false",
      "outForDelivery": "false",
      "delivered": "false",
      "deliveryPerson": "",
      "deliveryPhone": ""
    },
    {
      "giftType": "Night Gown",
      "giftSize": "Med",
      "selected": "false",
      "checkedIn": "false",
      "outForDelivery": "false",
      "delivered": "false",
      "deliveryPerson": "",
      "deliveryPhone": ""
    }
  ],
  "enteredBy": "**********@gmail.com",
  "addedOn": "2016-07-03T22:41:08.348Z",
  "editedBy": "********@gmail.com",
  "lastEditedOn": "2016-07-03T22:41:22.642Z"
}

My issue is in getting the two gift items to show in my table.

I’m currently trying gifts.giftType and gifts.giftSize just to get things working, but that’s now showing anything for those columns. Much less, I’m not sure how to get both gifts to show, not how best to display them yet once I figure out how to get them to show.

As always, any help is appreciated. Meanwhile my internet search for hints will continue.


#2

Ok, i got this much

I can do gifts.[0].giftType and I get the first gift type for each recipient, but how can I loop through these within that recipients row, or at the very least show 2 rows per recipient using spacebars?


#3

The {{#each}} operator works equally well with arrays, so you can do something like this:

<template name="gifts">
  {{#each docs}}
    <div>{{name.first}} {{name.last}}</div>
    {{#each gifts}}
      <div>{{giftType}}</div>
    {{/each}}
  {{/each}}
</template>

#4

This was perfect! I now get those items listed out properly, thank you so much @robfallows.