流星:在模板中显示数据时出错

时间:2018-10-21 12:00:19

标签: meteor

我是Meteor的新手,尝试显示mongodb集合中的项目列表时遇到此错误。

error

这些是我要从我的rewards集合中检索的数据

rewards collection

这是我的代码:

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}}当前仅接受数组,游标或假值。” ...感谢您为解决此问题提供的帮助。

1 个答案:

答案 0 :(得分:1)

解决方案:我终于找到了错误。问题是我在加载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');

此问题与Default file load order in Meteor

有关