akaHeimdall
akaHeimdall

Reputation: 959

Meteor helpers not working in each loop

What should seem rather elementary is not working properly for me. I'm simply trying to show a list of announcements on a dashboard, but my helper does not seem to pull the data from the publication. My files are below.

publications.js

Meteor.publish('announcements', function() {
    return Announcements.find();
});

Template JS (dashboard.js):

Template.sendersDashboard.helpers({
    announcements: function() {
        return Announcements.find({}, {sort: {createdAt: -1}});
    }
})

View JS (dashboard.html):

<template name="dashboard_announcements">
    {{#each announcements}}
        {{> single_announcement}}
    {{else}}
        There are no Announcements to display.
        <br>
        <h5><a href="{{pathFor 'newAnnouncement'}}">Why don't you make one now?</a></h5>
    {{/each}}
</template>

When I view the page in the browser I only see the {{else}}case. I've checked the database and can see records available. Plus I am receiving no errors at all regarding the calls.

Any help, suggestions, etc. is greatly appreciated.

Upvotes: 0

Views: 478

Answers (2)

Tomas Hromnik
Tomas Hromnik

Reputation: 2200

You have different template name in your JS and HTML:

Your template name in JS is sendersDashboard:

Template.sendersDashboard.helpers({
   //code
});

And your template name in HTML is dashboard_announcements:

<template name="dashboard_announcements">
  <!-- code -->
</template>

I recommend to use camelCase names of your templates, so fix the HTML name:

<template name="sendersDashboard">
  <!-- code -->
</template>

Upvotes: 0

lehtu
lehtu

Reputation: 907

Just like Sindis suggested, you might be missing the subscription in your dashboard.js.

Meteor.subscribe('announcements');

Or another thing could be that you are having helper in wrong template. Instead of:

Template.sendersDashboard.helpers({...

You should have:

Template.dashboard_announcements.helpers({...

Upvotes: 2

Related Questions