1

似乎我无法在我的助手中访问 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

4

2 回答 2

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

于 2016-03-05T19:26:41.107 回答
0

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

var users = Meteor.users.find(filterObject, { sort: { 'profile.firstname': 1 } });
if( users ) {
  return users.fetch();
}
于 2016-03-05T22:14:44.540 回答