我有一个使用 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、帮助程序和路由的官方文档,但我就是找不到解决方案。有什么建议吗?
最佳答案
可以按任意字段查询。帮助器返回未定义,因为它没有找到任何匹配的内容。
这段代码有问题:
Meteor.publish('theCase', function (id) {
return Cases.findOne({
id: id
});
});
应该是:return Cases.find({id: id});
发布必须返回游标或调用 this.ready()
关于javascript - 如何在 Meteor 中通过任意属性(但不是 _id)查找一个集合项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32438526/