How can I post the same collection with different subsets of the data?

I have a collection called Rooms, now I want to publish different subsets of a room depending on where the user is in the application, so I meant this:

  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 information when the user is just browsing the rooms, however, when the user enters the room, I want to subscribe to him in the room so he can see who is in the room, and when the room is playing, subscribe to roomGame. which contains information about the game. This is how I subscribe (at this point, the user has 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 appear as undefined.

What am I missing?

+3


source to share


1 answer


Your route logic looks good. I would only change 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 I changed was to use Meteor.subscribe()

instead this.subscribe()

in the route parameter definition waitOn

. This is the prescribed way of defining a subscription descriptor in the route parameter definition waitOn

in the documentation here .

The second thing I changed was to wrap all your code in a route parameter definition data

in a statement if(this.ready()){}

. In the documentation here , the call is this.ready()

directly bound and responds to a route waitlist that is populated when you define one or more Meteor.subscribe()

calls in your route parameter definition waitOn

.

0


source







All Articles