meteor - Meteor:在模板中显示数据时出错
问题描述
我是 Meteor 的新手,当我尝试显示 mongodb 集合中的项目列表时出现此错误。
这些是我试图从我的rewards
收藏中检索的数据
这是我的代码:
服务器/publications.js
Rewards = new Mongo.Collection('rewards');
Meteor.publish('allRewards', function () {
if (this.userId) {
return Rewards.find({}, {
fields: {
'title': 1,
'headline': 1,
'summary': 1,
'description': 1,
'requirements': 1
}
})
} else {
this.ready()
}
});
lib/router.js
Router.route('/rewards', function () {
var selfRoute = this;
var rew = RewardsSubs.subscribe("allRewards");
document.title = "Rewards"
Tracker.autorun(function (computation) {
if (RewardsSubs.ready()) {
selfRoute.render('rewards', {
data: function () {
return {
rewards: rew
};
}
});
computation.stop()
} else {
selfRoute.render('loading');
}
});
});
客户端/启动/default.js
RewardsSubs = new SubsManager({
// maximum number of cache subscriptions
cacheLimit: 10,
// any subscription will be expire after 5 minute, if it's not subscribed again
expireIn: 10
});
客户端/模板/rewards/rewards.html
<template name="rewards">
<div class="ui container">
<table class="ui very basic table">
<tbody>
{{#each rewards}}
<tr>
<td>
<span>{{title}}</span>
</td>
<td>
<h4 class="ui image header">
<div class="content">
<div class="header">
. <span>{{headline}}</span>
. <span>{{summary}}</span>
</div>
</div>
</h4>
</td>
</tr>
{{/each}}
</tbody>
</table>
</div>
</template>
我不知道为什么我有这个“{{#each}} 当前只接受数组、游标或错误值。”......我感谢任何帮助解决这个问题。
解决方案
解决方案:我终于找到了错误。问题是我在server/publications.js
加载之前引用了 Rewards 集合,因此尚未创建 Rewards 集合。
首先,我按照@iiro 在评论中的建议更改了 router.js。
lib/router.js
Router.route('/rewards', function () {
var selfRoute = this;
RewardsSubs.subscribe("allRewards");
document.title = "Rewards"
Tracker.autorun(function (computation) {
if (RewardsSubs.ready()) {
selfRoute.render('rewards', {
data: function () {
return {
rewards: Rewards.find()
};
}
});
computation.stop()
} else {
selfRoute.render('loading');
}
});
});
然后我将奖励集合从文件夹更改server/publications.js
为lib
lib/collections/rewards.js
Rewards = new Mongo.Collection('rewards');
推荐阅读
- android - 如何以编程方式更改 RecyclerView 的 fastScrollEnabled 属性?
- sql - Oracle 将多行转置为列,以便每个组的数据彼此相邻可见
- regex - Make regex quantifier length depend on previous capture group
- react-native - React Native 项目没有与 FlatList 正确分层
- javascript - 如何检查瓦片是否已经用 JavaScript 中的值填充?
- python - 在 Groupby 之后将两个数据框列除以订购时间
- terraform - 如何在 terraform 状态文件中填充新的输出变量?
- javascript - 如何在 ASP.NET Web 应用程序的 C# 代码中集成 chart.js 线性仪表或圆形仪表
- jquery - 这个 jQuery 功能是否存在?
- mysql - MySQL 到 Microsoft SQL Server 派生表语法