Search code examples
meteoriron-router

iron router syntax: onAfterAction


Router.route('/courses/:_catalog', function () {
  var courseCatalog = this.params._catalog.toUpperCase();

  Meteor.subscribe("courseCatalog", courseCatalog);

  this.render('CourseDetail', {
    to: 'content',
    data: function () {
      return Courses.findOne({catalog: courseCatalog});
    }
  });
}, {
  onAfterAction: function() {
    if (!Meteor.isClient) {
      return;
    }
    debugger
    var course = this.data(); <======
    SEO.set({
      title: "course.catalog"
    });
  }
});

In the above code, please look at the debugger statement. I want to access the data but it seems I am doing something wrong because this.data doesn't exist. I also tried Courses.find().fetch() but I only get an empty array inside onAfterAction. What's the right syntax and what am I missing?


Solution

  • It needs to be inside a this.ready() block:

      onAfterAction: function() {
        if (this.ready()) {
          var course = this.data(); 
          ...
        }
      }