-1

I have an embedded document in a MongoDB collection "Author" as such:

{"Name": "John Doe",
 "Country": "U.S.A", 
 "Books": [
    {"BName": "Book1", "Year": "1950"},
    {"BName": "Book2", "Year": "1960"}
  ]
}

I want to access the Books data, loop through it and display each Book in a table.

This is what my JS file looks like

Template.Author.helpers({  
   author: function() {
       //_id of the Author is passed via the URL
       return Author.find({"_id": FlowRouter.getParam('_id')})
   }
 });

This is the HTML for my report

<template name="Author">
<body>
  <div class="row">
    <div class="col-md-2">
      Book Name
    </div>
    <div class="col-md-2">
      Year
    </div>
  </div>
  {{#each author}}
    {{> bookdetails}}
  {{/each}}
</body>
</template>

<template name="bookdetails">
  <div class="row">
    <div class="col-md-2">
      {{Books.BName}}
    </div>
    <div class="col-md-2">
      {{Books.Year}}
    </div>
  </div>
</template>

This works when I have only one record in the embedded Books document but not when I have more than one record - which makes sense since 'Books.BName' is ambiguous at that point.

I need to loop through 'Books' and output each BName and Year. This answer is the closest I found to doing this but I get the error: TypeError: _.value is not a function This may be because I have an embedded document as opposed to an array.

Community
  • 1
  • 1

1 Answers1

0

Your naming is confusing. You've got Book helper that returns an author, and that's what you're passing to the bookdetails template. You should rename the helper to author to reduce confusion. Then you can loop through the actual array of books with {{#each author.Books}}.

Furthermore, in the bookdetails template access the book parameters directly, e.g. {{BName}} instead of {{Books.BName}}.

Hubert OG
  • 19,314
  • 7
  • 45
  • 73
  • Thanks @hubert-og. Good point about naming - I have modified that in the question. `{{#each author.Books}}` is, directionally, what I was looking for but making that change and changing to `{{BName}}` (which makes sense once you iterate through author.Books) is not working for me (returns no data). – Bhavin Vyas Apr 23 '16 at 13:50
  • I didn't notice you're doing `Author.find` as well. Change that to `Author.findOne` together with the other tweaks. – Hubert OG Apr 23 '16 at 14:21
  • Thanks, this works! However, it fails when there is only one book for a given Author (as opposed to more than one when it works). I get the following error: `{{#each}} currently only accepts arrays, cursors or falsey values.` – Bhavin Vyas Apr 23 '16 at 19:55
  • That's probably because you don't follow the same schema in such case, and pass the single book as an object instead of an array containing single object. It's better to hold on to a single schema. – Hubert OG Apr 23 '16 at 21:03
  • Perfect, Thanks so much! It works now. I spent many days trying to get this right - very much appreciate your help and patience! – Bhavin Vyas Apr 23 '16 at 21:29