Search code examples
javascriptmeteoriron-routerrouterreferenceerror

Meteor: "ReferenceError: myAdminHookFunction is not defined"


Following Meteor Tutorial by Matthew Platts.

In Chapter 4, section 4.2.5 Force Login, when I implement the code, I get the following error:

Your app is crashing. Here's the latest log.


/Users/TXC/.meteor/packages/meteor-tool/.1.1.3.1wysac9++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:245
                        throw(ex);
                              ^
ReferenceError: myAdminHookFunction is not defined
    at app/both/router.js:31:23
    at app/both/router.js:33:3
    at /Users/TXC/code/foosboom-meteor/.meteor/local/build/programs/server/boot.js:222:10
    at Array.forEach (native)
    at Function._.each._.forEach (/Users/TXC/.meteor/packages/meteor-tool/.1.1.3.1wysac9++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/underscore/underscore.js:79:11)
    at /Users/TXC/code/foosboom-meteor/.meteor/local/build/programs/server/boot.js:117:5
Exited with code: 8
Your application is crashing. Waiting for file change.

UPDATE: everything is working fine until I update code as suggested in Listing 4.26: both/router.js.

UPDATE 2: here is the content of the router.js file:

Router.configure({
  layoutTemplate: 'layout',
  loadingTemplate: 'loading'
});

Router.route('/', {
  name: 'games',
  waitOn: function(){
    return [Meteor.subscribe("games"), Meteor.subscribe("teams")];
  }
});

Router.route('/teams', {
  waitOn: function(){
    return Meteor.subscribe("teams");
  }
});

var requireLogin = function(){
  if(!Meteor.user()){
    if(Meteor.loggingIn()){
      this.render("loading");
    } else {
      this.render("accessDenied");
    }
  } else {
    this.next();
  }
}

Router.onBeforeAction(requireLogin);

I went through my code three times and did not see what was going wrong: any idea?


Solution

  • Looks like there's a typo in that tutorial:

    Router.onBeforeAction(myAdminHookFunction, {
     only: ['admin']
     // or except: ['routeOne', 'routeTwo']
    });
    

    Should look like this:

    Router.onBeforeAction(requireLogin, {
     only: ['admin']
     // or except: ['routeOne', 'routeTwo']
    });
    

    so myAdminHookFunction should be replaced with requireLogin.