【问题标题】:How to access FlowRouter subscriptions in Meteor template helpers?如何在 Meteor 模板助手中访问 FlowRouter 订阅?
【发布时间】:2016-03-05 15:53:24
【问题描述】:

似乎我无法在我的助手中访问 FlowRouter 模板订阅。你怎么能这样做?

在我的服务器代码中:

Meteor.publish('AllUsers', function() {
    return Meteor.users.find({}, {fields: {profile: 1}});
})

在我的路由器代码中:

var userRoutes = FlowRouter.group({
    subscriptions: function(params, queryParams) {
        this.register('AllUsers', Meteor.subscribe('AllUsers'));
    },
});

在我的模板代码中:

{{#if checkFlowRouterSubs}}
    {{#each getTheUsers}}
        {{>userPartial}}
    {{/each}}
{{/if}}

在我的助手中,我有“守卫”:

checkFlowRouterSubs: function() {
    if (FlowRouter.subsReady()) {
        return true;
    };
    return false;
},

然后是 getTheUsers 助手:

...
var users = AllUsers.find(filterObject, { sort: { 'profile.firstname': 1 } }).fetch(); // the actual query definitely works
...

但我得到一个错误:

Exception in template helper: ReferenceError: AllUsers is not defined

我应该注意,在 getTheUsers 帮助器中,FlowRouter.subsReady('AllUsers') 返回 true

【问题讨论】:

    标签: meteor meteor-blaze flow-router


    【解决方案1】:

    所以,首先,这个:

    var userRoutes = FlowRouter.group({
        subscriptions: function(params, queryParams) {
            this.register('AllUsers', Meteor.subscribe('AllUsers'));
        },
    });
    

    不是服务器代码:它是客户端代码:Flow-router 是客户端路由器:与直觉相反,但这是所有这些路由器的基础。 这里的提示是您正在“订阅”此代码中的发布,因此它在客户端。

    Iron-Router 在服务器端和客户端都进行路由,所以当你从那里来时,它会让事情变得更加混乱。

    这里缺少的是服务器端的publish 函数。

    Meteor.publish('AllUsers', function() {
        return AllUsers.find();
    });
    

    编辑:

    错误

    Exception in template helper: ReferenceError: AllUsers is not defined 好像是因为您没有在客户端定义集合

    var AllUsers = Mongo.Collection('AllUsers'); //or whatever the actual collection

    【讨论】:

    • 感谢您的回复。为那个错误道歉——我的服务器代码中确实有这个错误。我已经修改了问题
    • 您的收藏似乎没有在客户端定义。
    • 谢谢,这可能是原因。我猜您仍然需要客户端的本地存储来存储已发布的集合
    【解决方案2】:

    当您尝试从订阅中获取数据时,您希望调用您要为其获取数据的实际集合,而不是订阅名称。在这种情况下,我认为您的意思是 Meteor.users:

    var users = Meteor.users.find(filterObject, { sort: { 'profile.firstname': 1 } });
    if( users ) {
      return users.fetch();
    }
    

    【讨论】:

    • 谢谢。当我在助手中执行此操作时,有时会出现未定义的错误,即使使用警卫也是如此
    猜你喜欢
    • 1970-01-01
    • 2016-03-04
    • 2016-09-20
    • 2016-01-03
    • 2013-02-14
    • 2015-03-28
    • 2014-05-12
    • 2016-03-02
    • 2016-06-10
    相关资源
    最近更新 更多