3

I see older posts where old versions of Iron-router do wait() in before:

before: function() {
      // let's make sure that the topPosts subscription is ready and the posts are loaded
      if (this.data()) {
        // we can then extract the userIds of the authors
        var userIds = this.data().map(function(p) { return p.userId });
        // and add the authors subscription to the route's waiting list as well
        this.subscribe('authors', userIds).wait(); **<--- this guy!**
      }
    }

Above is from https://www.discovermeteor.com/blog/reactive-joins-in-meteor/

If I add wait() to my OnBeforeAction subscribe, I get these errors:

You called wait() after calling ready() inside the same computation tree.

You can fix this problem in two possible ways:

1) Put all of your wait() calls before any ready() calls.
2) Put your ready() call in its own computation with Deps.autorun.

My waitOn is

waitOn: function() {
            return Meteor.subscribe('weeks', this.params.league);
        },

and OnBeforeAction

onBeforeAction: function() {
            if (this.ready()) {
                // we can now get the latest (first in the list) week
                var week = SheetData.find().fetch()[0].week;
                this.subscribe('standings', this.params.league, week).wait();
                this.next();

            }
        }

If I remove the wait() the render template starts before my subscription is ready. The suggested fixes don't seem applicable. What am I missing?

Pablo
  • 2,376
  • 5
  • 26
  • 34

1 Answers1

0

Here's a solution based on radzserg's answer to a similar question. He cleverly uses a callback function in the original waitOn function, and no onBeforeAction. In your case it would be:

waitOn: function() {
    var that = this;
    return Meteor.subscribe('weeks', this.params.league, function() {
        var week = SheetData.find().fetch()[0].week;
        that.wait(Meteor.subscribe('standings', that.params.league, week));
    });
}
Community
  • 1
  • 1
Racing Tadpole
  • 4,270
  • 6
  • 37
  • 56