iron-meteor / iron-router

A client and server side router designed specifically for Meteor.
MIT License
1.98k stars 412 forks source link

Defining routes using arrow functions doesn't work #1597

Open Kamaropoulos opened 7 years ago

Kamaropoulos commented 7 years ago

Defining a route using the following code:

Router.route('/', () => {
    this.render('myTemplate');
});

will print an error in the console:

Route dispatch never rendered. Did you forget to call this.next() in an onBeforeAction?

while the same code but with a regular function callback will work as expected:

Router.route('/', function() {
    this.render('myTemplate');
});

I know that the default way to define routes is with regular functions but shouldn't this theoretically work?

zimme commented 6 years ago

No, because render is attached to this which is bound when using an arrow function. i.e. an arrow function won't get the special this context that iron-router gives to the route handlers.