问题
I'm getting frustrated at an 'Exception in template helper' error in a Meteor application I'm trying to develop.
In /lib/collections.js I have:
Categories = new Meteor.Collection("categories");
Venues = new Meteor.Collection("venues");
VenuesAndUsers = new Meteor.Collection("venuesAndUsers");
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();
});
In /lib/router.js I have:
Router.configure({
// Other stuff
waitOn: function() {
return [
Meteor.subscribe('Categories'),
Meteor.subscribe('Venues'),
Meteor.subscribe('VenuesForUser'),
];
}
});
In /client/templates/list.html I have:
{{#each xzz}}
{{name}} - {{id}}<br />
{{/each}}
{{#each venues}}
{{venueId}} - {{userId}}<br />
{{/each}}
In /client/templates/list.js I have:
venues: function() {
return VenuesForUser.find();
},
xzz: function() {
return Venues.find();
}
My output is:
Venue 1 - Venue 1 id
Venue 2 - Venue 2 id
...
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
...
Now, my issue is that my publishing 'VenuesFoUser' generates the error above when trying to access its contents.
But, why!?
Edit: 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?
回答1:
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.
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.
I hope I made it clear.
回答2:
It needs to be, not Meteor, but Mongo, such as:
Platypi = new Mongo.Collection('platypi');
not:
Platypi = new Meteor.Collection('platypi');
来源:https://stackoverflow.com/questions/33177382/issue-with-accessing-subscriptions-in-meteor