【问题标题】:Meteor: error when displaying data in templateMeteor:在模板中显示数据时出错
【发布时间】:2018-10-24 13:42:04
【问题描述】:

我是 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
});

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

【问题讨论】:

  • 你的模板代码在哪里?
  • 无论如何,我认为你的回报 { 奖励:rew };应该是 return { reward: Rewards.find() };
  • 对,对不起,我忘记了。我刚刚添加了模板。关于您的第二条评论,我已尝试使用ReferenceError: Rewards is not defined,但出现“ReferenceError:未定义奖励”。这个Rewards不是指我在publications.js中定义的集合吗?

标签: meteor


【解决方案1】:

解决方案:我终于找到了错误。问题是我在加载 server/publications.js 之前引用了 Rewards 集合,因此尚未创建 Rewards 集合。

首先,我按照@iiro 在 cmets 上的建议更改了 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');
        }
    });
});

然后我将 Rewards 集合从 server/publications.js 更改为 lib 文件夹

lib/collections/rewards.js

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

这个问题与Default file load order in Meteor有关

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-03
    • 2012-03-18
    • 2019-08-21
    • 1970-01-01
    • 2016-04-22
    • 2021-12-17
    • 1970-01-01
    相关资源
    最近更新 更多