问题描述
我对我正在尝试开发的 Meteor 应用程序中的模板助手中的异常"错误感到沮丧.
I'm getting frustrated at an 'Exception in template helper' error in a Meteor application I'm trying to develop.
在/lib/collections.js 我有:
In /lib/collections.js I have:
Categories = new Meteor.Collection("categories");
Venues = new Meteor.Collection("venues");
VenuesAndUsers = new Meteor.Collection("venuesAndUsers");
在/server/main.js 我有:
In /server/main.js I have:
Meteor.publish("Categories", function () {
return Categories.find({}, {sort: {order: 1}});
});
Meteor.publish("Venues", function () {
return Venues.find();
});
Meteor.publish("VenuesForUser", function () {
return VenuesAndUsers.find();
});
在/lib/router.js 我有:
In /lib/router.js I have:
Router.configure({
// Other stuff
waitOn: function() {
return [
Meteor.subscribe('Categories'),
Meteor.subscribe('Venues'),
Meteor.subscribe('VenuesForUser'),
];
}
});
在/client/templates/list.html 我有:
In /client/templates/list.html I have:
{{#each xzz}}
{{name}} - {{id}}<br />
{{/each}}
{{#each venues}}
{{venueId}} - {{userId}}<br />
{{/each}}
在/client/templates/list.js 我有:
In /client/templates/list.js I have:
venues: function() {
return VenuesForUser.find();
},
xzz: function() {
return Venues.find();
}
我的输出是:
Venue 1 - Venue 1 id
Venue 2 - Venue 2 id
...
在 javascript 控制台中,我得到:
And in the javascript console, I get:
Exception in template helper: .venues@http://localhost:3000/app/client/templates/list.js?2a82ae373ca11b4e9e171649f881c6ab1f8ed69b:11:7
bindDataContext/<@http://localhost:3000/packages/blaze.js?695c7798b7f4eebed3f8ce0cbb17c21748ff8ba8:2994:14
...
现在,我的问题是我的发布VenuesFoUser"在尝试访问其内容时产生了上述错误.
Now, my issue is that my publishing 'VenuesFoUser' generates the error above when trying to access its contents.
但是,为什么!?
如果我将VenuesForUser"的所有实例更改为VenuesAndUsers",则订阅有效.但这有什么意义呢?我可以只为与集合匹配的订阅命名吗?
If I change all instances of 'VenuesForUser' to 'VenuesAndUsers' the subscription works. But how does that make sense? Can I only give names to subscriptions that match collections?
推荐答案
出版物将文档发布到您的收藏中.发布可以命名为 random
,但如果它从名为 NotRandom
的集合返回一个游标,那么这就是它们在客户端上发布到的集合.
Publications publish documents to your collections. A publication can be named random
, but if it returns a cursor from a collection named NotRandom
, then that's the collection they get published to on the client.
您有一个名为 VenuesForUser
的发布,它从集合 VenuesAndUsers
返回一个游标.在客户端,VenuesAndUsers
是包含从服务器发布的数据的集合.所以你只能做 VenuesAndUsers.find()
,因为没有名为 VenuesForUser
的集合.出版物名称对其他任何内容都没有影响 - 它只是一个名称.它不会为您创建新集合.
You have a publication named VenuesForUser
, which returns a cursor from the collection VenuesAndUsers
. On the client side, VenuesAndUsers
is the collection that has the data that was published from the server. So you can only do VenuesAndUsers.find()
, since there is no collection named VenuesForUser
. The publication name has no effect on anything else - it's just a name. It doesn't create a new collection for you.
我希望我说清楚了.
这篇关于在 Meteor 中访问订阅的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!