0

I started my first app with Meteor and everything was going fine, until I noticed that a collection is no longer showing in a template.

This is my code:

App.js

Tasks = new Mongo.Collection("tasks");

if (Meteor.isServer) {
    Meteor.publish("tasks", function () {
        return Tasks.find();
    });
}

if (Meteor.isClient) {

Meteor.subscribe("tasks");

Template.body.helpers({
    tasks: function () {
        return Tasks.find({}, {sort: {createdAt: -1}});
    }
});
}

App.html

<template name="home">

    <h3>Open Tasks</h3>

          <ul>
            {{#each tasks}}
                {{> displayTasks}}
            {{/each}}
          </ul>

</template>

<template name="displayTasks">

    <li>{{text}}</li>

</template>

Routes.js

Router.route("/", function () {
    this.render("Home");
});

I've double-checked with http://meteortips.com/second-meteor-tutorial/publish-subscribe/ and MeteorJS template not showing data, not appearing and it seems that I've done everything correctly. I also tried changing the names and trying to display the tasks on different templates, but to no avail. MongoDB does have data and I can manually insert entries (using the Mongo console) that have their text fields filled out.

What else can I do to troubleshoot? Backtracing my steps hasn't helped and I'd rather avoid starting a new app from scratch.

NOTE: I'm using Iron Router.

Community
  • 1
  • 1
Optimae
  • 942
  • 1
  • 12
  • 23
  • `Template.home.helpers` instead of `Template.body.helpers`. Also you can check if the docs are published by opening your browser console and typing `Tasks.find().count()`. – David Weldon Dec 11 '15 at 05:26
  • Wow, that fixed it completely, thanks @David. I can't believe I missed something that important, should have RTFM properly. If you add it as an answer I'll mark it as correct. – Optimae Dec 11 '15 at 07:14

1 Answers1

0

By default, child templates don't have access to their parent's helpers. In the code above, the tasks helper has been defined for the body template, but it's needed in the home template. Try this instead:

Template.home.helpers({
  tasks: function () {
    return Tasks.find({}, {sort: {createdAt: -1}});
  }
});

Note that you can explicitly pass a helper from a parent to a child, as seen in my answer to this question, but that's uncommon.

David Weldon
  • 63,632
  • 11
  • 148
  • 146