I have collections that are published on the server side, and use iron router's waitOn to subscribe to these collections. However, on the client side I can never see any reference to the collections defined on the server side. The only way I can access them is by defining the collection on the client side (devices = new Meteor.Collection('devices')), but I don't see anyone doing this in their examples online. Here is the code:
Client Code:
Router.route('/devices', {
waitOn: function() {
return [
Meteor.subscribe('devices', Meteor.user()._id),
];
},
action: function() {
this.render();
}
});
Server Side:
Devices = new Mongo.Collection("devices");
Devices.allow({
'insert': function (userId, doc) {
if (userId === doc.accountId) {
return true;
}
return false;
},
'update': function (userId, doc) {
if (userId === doc.accountId) {
return true;
}
return false;
},
});
Meteor.publish('devices', function(id) {
return Devices.find({accountId: id});
});
I have removed autopublish, and from the examples online, I should just be able to reference Devices.find({}). Instead, I have to use devices = new Meteor.Collection('devices') which then causes issues since if I call this again, it will say I already have a collection called devices. Does anyone know why I can't reference Devices?
The reason you can't reference it on the client side is that you haven't made the collection available to the client.
Leave the .allow
and .publish
methods on the server side, but move your collection creation to the lib folder to make it available on both the client and server.
/lib/collections.js:
Devices = new Mongo.Collection("devices");