I have collection called Rooms, now, I want to publish different subsets of Room depending on where the user is on the app, so, this is what I had in mind:
Meteor.publish('roomList', function() {
return Rooms.find({}, {fields: {'roomId': 1, 'playerCount': 1, 'isPlaying': 1}});
});
Meteor.publish('roomInfo', function(_id) {
return Rooms.find({_id: _id}, {fields: {'players': 1}});
})
Meteor.publish('roomGame', function(_id) {
return Rooms.find({_id: _id}, {fields: {'game': 1}});
})
I don't want to throttle the connection sending players info when the user is just browsing rooms, however, when a user enters a room, I want to subscribe him to roomInfo so he can see who's in the room, and when the room is playing, subscribe to roomGame which contains game info. This is how I subscribe(at this point, user is already subscribed to roomList).
Router.route('/sala/:_id', {
name: 'roomLobby',
waitOn: function() {
return this.subscribe('roomInfo', this.params._id);
},
data:function(){
var r = Rooms.findOne(this.params._id);
console.log(r.players);
return r;
}
});
However, r.players always come up as undefined.
What am I missing?
Your route logic looks pretty good. I would change only a couple of things:
Router.route('/sala/:_id', {
name: 'roomLobby',
waitOn: function() {
return Meteor.subscribe('roomInfo', this.params._id);
},
data: function() {
if(this.ready()) {
var r = Rooms.findOne(this.params._id);
console.log(r.players);
return r;
}
}
});
The first thing that I changed is to use Meteor.subscribe()
instead of this.subscribe()
in your waitOn
route option definition. This is the prescribed way to define a subscription handle in a waitOn
route option definition per the documentation here.
The second thing that I changed is to wrap all of your code in the data
route option definition in an if(this.ready()){}
statement. Per documentation here, the this.ready()
call is tied directly into and reacts to the wait list for the route, which is filled as you define one or more Meteor.subscribe()
calls in your waitOn
route option definition.