首页 > 解决方案 > 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}} 当前只接受数组、游标或错误值。”......我感谢任何帮助解决这个问题。

标签: meteor

解决方案


解决方案:我终于找到了错误。问题是我在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.jslib

lib/collections/rewards.js

Rewards = new Mongo.Collection('rewards');

此问题与Meteor 中的默认文件加载顺序有关


推荐阅读