Meteor Iron-router onBeforeAction this.next undefined
Asked Answered
R

1

6

I've got the following route configuration: https://gist.github.com/chriswessels/76a64c421170095eb871

I'm getting the following error when attempting to load a route:

Exception in defer callback: TypeError: undefined is not a function
at manageLoadingIndicator (http://localhost:3000/both/router/routes.js?ef701fada29363a443a214f97988ce96ebaec025:30:10)
at RouteController.runHooks (http://localhost:3000/packages/iron_router.js?da7f2ac81c3fd9daebf49ce9a6980a54caa1dc17:843:16)
at http://localhost:3000/packages/iron_router.js?da7f2ac81c3fd9daebf49ce9a6980a54caa1dc17:2302:14
at Tracker.Computation._compute (http://localhost:3000/packages/tracker.js?192a05cc46b867dadbe8bf90dd961f6f8fd1574f:288:36)
at new Tracker.Computation (http://localhost:3000/packages/tracker.js?192a05cc46b867dadbe8bf90dd961f6f8fd1574f:206:10)
at Object.Tracker.autorun (http://localhost:3000/packages/tracker.js?192a05cc46b867dadbe8bf90dd961f6f8fd1574f:476:11)
at http://localhost:3000/packages/iron_router.js?da7f2ac81c3fd9daebf49ce9a6980a54caa1dc17:2279:12
at Utils.extend._run.withNoStopsAllowed (http://localhost:3000/packages/iron_router.js?da7f2ac81c3fd9daebf49ce9a6980a54caa1dc17:2248:21)
at Tracker.Computation._compute (http://localhost:3000/packages/tracker.js?192a05cc46b867dadbe8bf90dd961f6f8fd1574f:288:36)
at new Tracker.Computation (http://localhost:3000/packages/tracker.js?192a05cc46b867dadbe8bf90dd961f6f8fd1574f:206:10)

It's talking about the following line, which is in a onBeforeAction hook:

function manageLoadingIndicator (pause) {
  if (this.ready()) {
    Session.set('loading', false);
    this.next(); // THIS LINE HERE
  } else {
    Session.set('loading', true);
    pause();
  }
}

Why is this.next undefined? Help please!

Chris

Rochet answered 8/10, 2014 at 11:39 Comment(1)
I'm having the same problem Chris. I'll let you know if I figure it out. Please do the same for me. Thx.Karalee
C
3

You are mixing up different versions of Iron router:

Prior to iron router 1.0, onBeforeAction would proceed to action unless pause (the first arg to onBeforeAction is invoked. There is no .next() method.

From 1.0 onwards this has been changed. pause() is no longer passed as an argument. This is where the .next() method replaces it.

You are evidently running on an old version of iron router, so therefore your hook should look like this:

function manageLoadingIndicator (pause) {
  if (this.ready()) {
    Session.set('loading', false);
  } else {
    Session.set('loading', true);
    pause();
  }
}

Once you upgrade iron router, you need to change it to this:

function manageLoadingIndicator () {
  if (this.ready()) {
    Session.set('loading', false);
    this.next();
  } else {
    Session.set('loading', true);
  }
}
Cyclosis answered 9/11, 2014 at 17:34 Comment(1)
Thanks for the answer. You're correct that the iron-router API changed from 1.0 onwards, however there was a period of API flux, where the pause argument was still passed into the callback function alongside this.next. I believe this was version 0.9.4 (which was when I asked this question). I migrated the router setup to use this.next alone after 1.0 hit. To others: This involves ensuring all of your onBeforeAction hooks call this.next. Pausing a route's execution is opt-out rather than opt-in as it was before.Rochet

© 2022 - 2024 — McMap. All rights reserved.