流星助手在每个循环中都不能工作

Meteor helpers not working in each loop

本文关键字:循环 都不能 工作 流星      更新时间:2023-09-26

看起来相当基本的东西对我来说不能正常工作。我只是想在仪表板上显示一个公告列表,但是我的助手似乎没有从发布中提取数据。我的文件在下面

publications.js

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

模板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>

当我在浏览器中查看页面时,我只看到{{else}}案例。我已经查了数据库,可以看到可用的记录。另外,我没有收到任何关于电话的错误。

如有任何帮助、建议等,不胜感激。

就像Sindis建议的那样,您可能错过了dashboard.js中的订阅。

Meteor.subscribe('announcements');

或者另一件事可能是你在错误的模板中有帮助。而不是:

Template.sendersDashboard.helpers({...

你应该有:

Template.dashboard_announcements.helpers({...

你的JS和HTML中有不同的模板名称:

你的模板名称在JS是sendersDashboard:

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

和你的模板名称在HTML是dashboard_announcements:

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

我建议使用camelCase的模板名,所以修复HTML名称:

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