Search code examples
meteoriron-router

Meteor iron-route onBeforeAction


I have this simple Iron-router route in my Meteor application which I use whenever any user need to logout of the app, although whenever I call this route I get the following error. Can someone please tell me what I am doing wrong/ missing here? Thanks

On a minor note, Meteor.logout doesn't return any error

Router.route('/logout', function(){
  Meteor.logout(function(err){
      if(err){
        console.log('Error Logging out: '+ err);
      }
      this.redirect('home');
  });
});

Error:

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

Exception in delivering result of invoking 'logout': TypeError: undefined is not a function
    at http://localhost:3000/both/router/routes.js?8871acf5e06150f7af89862f68c245a05fe13db8:110:12
    at http://localhost:3000/packages/accounts-base.js?7c29db5c21a76726509bb6bb2a68a2b4b1ecf657:674:19

Solution

  • Scope in Meteor.logout callback is not the same scope as in Router.route callback. That's why you need to assign Router.route's scope to variable self and then use it inside Meteor.logout callback.

    Router.route('/logout', function(){
      var self = this;
      Meteor.logout(function(err){
          if(err){
            console.log('Error Logging out: '+ err);
          }
          self.redirect('home');
      });
    });