7

I've added iron-router to my app to handle routing between the home page, an about page and the main page of the app which is a map

After adding iron-router with meteorite, I wrote a router.js file and placed it in my /client folder, however I'm getting an error that says "Uncaught ReferenceError: Router is not defined "

I checked the error with chrome devtools and it pointed to "Router.configure(..." at the beginning of router.js which I've added below

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

Router.map( function () {
//the about route
this.route('about', {
    path: '/about',
    template: 'about',
    action: function () {
        console.log('now routing the about template');
    }
});

this.route('home', {
    path: '/',
    template: 'home',
    action: function () {
        console.log('now routing the home template');
    }
});

//the map route
this.route('map', {
    path: '/map',
    template: 'map',
    action: function () {
        console.log('now routing the map template');
    }

});
});

Does anyone know why I'm getting the error that Router is not defined?

wcjohnson11
  • 108
  • 1
  • 6
  • I moved the router.js file to the /lib directory so that it would load first. This broke the app so I then uninstalled and re-installed iron-router with meteorite and the Routing now works within my app. I hope this can help someone who is stuck in a similar situation. – wcjohnson11 Jan 22 '14 at 23:21

4 Answers4

6

Make sure your meteor version is 0.8.3 or up and use meteorite with command

mrt add iron-router

Others will cause iron router building errors.

Otherwise, check that your codes of router configure are in Meteor.isClient scope. If not, just wrap them in client scope.

if(Meteor.isClient){
  Router.configure({
   ......
  });

  Router.map(function(){
   ......
  });
}

If Iron-Router was installed below version 0.8.3, you should remove them from packages and smart.json, update meteor with command,

meteor update

and install iron router again with meteorite.
If no error messages show up, everything goes well

Leon Lin
  • 76
  • 1
  • 2
2

I've had similar issues adding iron-router to Meteor 0.8.3.

This recipe seems to work for me.

  • Add iron router to smart.json (or create if new project):

{"packages": { "iron-router": {"version":"0.8.2"} }}

  • meteor update

At this point iron-router is in the packages folder but may not listed in .meteor/packages list. If not:

  • meteor add iron-router

Hope this is helpful

VessoVit
  • 468
  • 5
  • 12
shoggs
  • 31
  • 5
1

I realize you fixed this, but for documentation purposes:

Putting Iron Router in lib/ directory will allow both client and server to access the code and get's loaded first.

See What are the best practices for structuring a large Meteor app with many HTML template files?

Community
  • 1
  • 1
hharnisc
  • 937
  • 7
  • 11
0

It is possible that iron-router is correctly installed but not configured to be used. It just happened to me and I used your question to understand a bit more my issue.

Actually, I had the package present in package and in the smart.json/lock but it wasn't used in the file .meteor/packages and wasn't loaded when meteor started. So it couldn't find it. Just adding the name of the package fixed it.

Vadorequest
  • 16,593
  • 24
  • 118
  • 215