Repeating Meteor template with a specific item in collection


#1

I have a collection with this item:

{
  "item1": ["foo", "bar", "baz"],
  "item2": ...
}

I made a helper function to repeat a template for each item in item1

Template.temp.helpers({
  items: function() {
    return Col.find({},{item1:1});
  }
});

and this is the template

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

But I get back an empty array. Why doesn’t it work?


#2

Because your helper “items” is in test but should be in temp.
Change “Template.test.helpers({” to “Template.temp.helpers({” and you should be good. Or change the name of the temp template to test.

To debug stuff like this, put a breakpoint in the helper when debugging and see if it is ever triggered.


#3

It’s a typo. The real names do match so this is not the problem. I think I am accessing the items wrong. I think I should be doing something like Col.find({},{item1:1})["item1"] but it doesn’t work


#4

What does “anotherTemplate” look like?


#5

If you want to iterate through ["foo", "bar", "baz"] you should do it with

Template.temp.helpers({
  items: function() {
    return (var _ref = Col.findOne({})) ? _ref.item1 : undefined;
  }
});

#6

This is not even valid Javascript

Although I did try return Col.findOne({}).item1 and it doesn’t work


#7

Does Col.findOne({}) or Col.findOne({}).item1 work from console?


#8

Hi,
http://meteorpad.com/pad/2X4GgKkePZTKBTQnr/print%20subitems


#9

You can start like this one and see if you have the results of the array. Once you get it then pass this to the other template

  <template name="temp">
  {{#each item1}}
    {{this}}
  {{/each}}
</template>

from above

{{anotherTemplate this}}