流星:在模板中显示数据时出错
Meteor: error when displaying data in template
我是 Meteor 的新手,当我尝试显示 mongodb 集合中的项目列表时出现此错误。
这些是我试图从我的 rewards
集合中检索的数据
这是我的代码:
server/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');
}
});
});
client/startup/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
});
client/templates/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}} 目前只接受数组、游标或错误值。”...我很感激任何解决这个问题的帮助。
SOLUTION: 我终于找到错误了。问题是我在加载 server/publications.js
之前引用了奖励 collection,因此尚未创建奖励 collection。
首先,我按照 @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');
}
});
});
然后我将奖励 collection 从 server/publications.js
更改为 lib
文件夹
lib/collections/rewards.js
Rewards = new Mongo.Collection('rewards');
我是 Meteor 的新手,当我尝试显示 mongodb 集合中的项目列表时出现此错误。
这些是我试图从我的 rewards
集合中检索的数据
这是我的代码:
server/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');
}
});
});
client/startup/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
});
client/templates/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}} 目前只接受数组、游标或错误值。”...我很感激任何解决这个问题的帮助。
SOLUTION: 我终于找到错误了。问题是我在加载 server/publications.js
之前引用了奖励 collection,因此尚未创建奖励 collection。
首先,我按照 @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');
}
});
});
然后我将奖励 collection 从 server/publications.js
更改为 lib
文件夹
lib/collections/rewards.js
Rewards = new Mongo.Collection('rewards');