Search code examples
meteoriron-router

subscription does not ready while subscribe to another


For example

Router.route('/:username/posts', {
  waitOn: function() {
    //Meteor.subscribe(('user', this.params.username));  // A
    var user = Meteor.users.findOne({username: this.params.username}); // B
    if(user) {
      return Meteor.subscribe('posts', user._id);
    }
  }
});

if I directly input http://localhost/userA/posts in the address bar of Chrome and enter, when code goes to B, at that time, Meteor.users has not ready.

How to handle this?


Solution

  • Do the user checking in the publish handler.
    Like this:

    Meteor.publish("posts", function (username) {
        var user = Meteor.users.findOne({username: username});
        if (user) {
            return Posts.find({user: user._id});
        }
    });