Totally confused about this.next() in Meteor iron-router
Asked Answered
G

1

8

I upgraded to Meteor 1.0, installed the latest iron-router package, tried to run my app and got this nice warning in my console log:

Route dispatch never rendered. Did you forget to call this.next() in an onBeforeAction?

So I tried to modify my routes according to the new version.

this.route('gamePage', {
        path: '/game/:slug/',
        onBeforeAction: [function() {
            this.subscribe('singlePlayer', this.params.slug).wait();
            var singlePlayer = this.data();
            if (singlePlayer) {
                if (singlePlayer.upgrade) {
                    this.subscribe('upgrades', this.params.slug).wait();
                    this.next();
                }
                this.next();
            }
            this.next();
        }],
        data: function() {
            return Games.findOne({slug: this.params.slug});
        },
        waitOn: function() { return [Meteor.subscribe('singleGame', this.params.slug)]}
    });

How can I fix this? Any help would be greatly appreciated.

Gunsmith answered 1/11, 2014 at 20:15 Comment(0)
C
9

Try removing all the .wait()s and removing the array around your onBefore function.

With the new API this.next() replaces .wait().

Chastity answered 1/11, 2014 at 23:20 Comment(4)
Thank you! That works! Do I need 3 x this.next() or just once at the end of the function?Gunsmith
You may not need it for the first if. Give it a try and report back.Chastity
This worked for me: if (singlePlayer) { if (singlePlayer.upgrade) { this.subscribe('upgrades', this.params.slug).wait(); } } this.next(); Could you please explain where I should place this.next() in general? Trial-and-error method is a little bit frustrating.Gunsmith
Sorry about that. Basically you need it if your onBefore or onRun does not result in a Router.go() being ran. Which yours does not. IronRouter onBefore will run the first command it encounters then stop... unless you call this.next(). Then it will do the same thing (run then stop) until either you reach the end of the onBefore/onRun logic or a Router.go is called.Chastity

© 2022 - 2024 — McMap. All rights reserved.