Repeating Meteor template with a specific item in collection

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?

1 Like

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.

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

What does “anotherTemplate” look like?

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;
  }
});

This is not even valid Javascript

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

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

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

1 Like

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}}