【发布时间】:2015-12-11 16:10:53
【问题描述】:
我使用 Meteor 启动了我的第一个应用程序,一切正常,直到我注意到模板中不再显示集合。
这是我的代码:
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");
});
我已经仔细检查了 http://meteortips.com/second-meteor-tutorial/publish-subscribe/ 和 MeteorJS template not showing data, not appearing 并且 似乎 我所做的一切都是正确的。我还尝试更改名称并尝试在不同的模板上显示任务,但无济于事。 MongoDB 确实有数据,我可以手动插入条目(使用 Mongo 控制台),它们的 text 字段已填写。
我还能做些什么来排除故障?回溯我的步骤并没有帮助,我宁愿避免从头开始一个新的应用程序。
注意:我正在使用 Iron Router。
【问题讨论】:
-
Template.home.helpers而不是Template.body.helpers。您还可以通过打开浏览器控制台并输入Tasks.find().count()来检查文档是否已发布。 -
哇,完全解决了,谢谢@David。我不敢相信我错过了一些重要的东西,应该正确地使用 RTFM。如果您将其添加为答案,我会将其标记为正确。
标签: javascript mongodb meteor