Search code examples
meteoriron-router

Cannot access params in Meteor with Iron Router


I have a list view of 'articles' and a detail view. These are the routes

 this.route("articles", {path: "/articles", controller: "ArticlesController"});
 this.route("article", {path: "/articles/:_id", controller: "ArticleController"});

Within the template for /articles, I have a link to the detail view

<a href="{{pathFor 'article' id=this._id }}"><i class="portlet-icon portlet-icon-maximize"></i></a>

The page is correctly rendered with the id embedded, eg:

<a href="/articles/gb6KgxbnfBeynz4rH">
   <i class="portlet-icon portlet-icon-maximize"></i>
</a>

In my controller associated with the route, I cannot access the params, it is empty.

this.ArticleController = RouteController.extend({
   template: "Article",

   onBeforeAction: function() {
       this.next();
   },

   action: function(){
      if (this.isReady()) {
         this.render();
      } else {
         this.render("loading");
      }
   },

   isReady: function() {
      var ready = true;
      var subs = [ Meteor.subscribe('singleArticle')];
      _.each(subs, function(sub) {
         if(!sub.ready())
            ready = false;
      });
      return ready;
   },

   data: function() {
      console.log(Iron.Location.get());
      console.log("params: " + this.params);

      return {
            article: Articles.findOne({articleId:this.params._id}, {})         
      };
   }
});

From the Javascript console I can see that the _id is passed with the url, but I cannot access the this.params

Javascript Console


Solution

  • The issue is that you're using console.log:

    console.log("params: " + this.params);
    

    The params object is actually an array with zero length. You can read a discussion about it here: https://github.com/iron-meteor/iron-router/issues/1213

    So you can use console.log on individual params and it will work:

    console.log("params._id: " + this.params._id);
    

    Or you can use:

    console.dir("params: " + this.params);