Search code examples
javascriptmeteoriron-routermeteor-helper

Meteor Iron-Router Registering helper with current route runs before router is loaded


Registering helper with current route returns error in console:

Exception in template helper: TypeError: Cannot read property 'getName' of undefined

And after Router is loaded - works fine. How to get rid of this console error?

Helper code:

if (Meteor.isClient) {

  // create global {{route}} helper
  Handlebars.registerHelper('route', function () {
    return Router.current().route.getName();
  });

}

Solution

  • Use technique called guarding:

        // create global {{route}} helper
      Handlebars.registerHelper('route', function () {
        return Router.current() && 
               Router.current().route &&  
               Router.current().route.getName &&
               Router.current().route.getName();
      });