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

172 views Asked by At

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();
  });

}
2

There are 2 answers

1
Kuba Wyrobek On BEST ANSWER

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();
  });
0
Billybobbonnet On

You should try to add an additional parameter in your template data in the onAfterAction hook of your route:

onAfterAction: function() {
  this.data.route = this.current().route.getName();
}

Once you have done that, you can access your route using yourTemplate.data.route

The code is untested.