1

我有一个使用 Flow Router 及其发布/订阅机制的应用程序。我还有一个集合和模板助手。代码在客户端

Template.theCase.helpers({
    theCase: function () {
        var id = FlowRouter.getParam('id');
        var theCase = Cases.findOne({
            id: id
        });

        return theCase;
    }
});

{{#with theCase}}
  {{ id }}
{{/with}}

然后,在服务器上

Meteor.publish('theCase', function (id) {
    return Cases.findOne({
        id: id
    });
});

最后,在两者上 ( lib)

FlowRouter.route('/case/:id', {
    subscriptions: function (params) {
        this.register('theCase', Meteor.subscribe('theCase', params.id));
    },
    action: function (params, queryParams) {
        return BlazeLayout.render('container');
    }
});

正如我所看到的,问题在于 helper 返回undefined,因为不允许通过 . 以外的任何其他属性在集合中查找项目_id。我怎样才能克服它?我已经阅读了大量关于 pub/sub、helpers 和 routing 的官方文档,但我找不到解决方案。有什么建议么?

4

1 回答 1

1

您可以按任何字段查询。助手返回 undefined 因为它没有找到任何匹配的东西。

这段代码有问题:

Meteor.publish('theCase', function (id) {
    return Cases.findOne({
        id: id
    });
});

它应该是:return Cases.find({id: id});

发布必须返回游标或调用this.ready()

于 2015-09-07T12:23:19.183 回答