Search code examples
javascriptmeteoriron-router

Meteor Iron Router : custom id route


Having a bit of trouble with iron router and passing in a custom id from a collection.

Some context: I have a collection of "groups" in which they all have their own special id other than the default _id that is auto generated. I am trying to make a route that is like

" localhost:3000/groups/:groupid "

so each group will have its own rendered template with the groups information.

HTML :

<template name="Group">
<h1>Group: {{groupName}}</h1>
</template>

CLIENTSIDE: grabbing a groupid from a session... I tested this and it works so its not an issue with grabbing it from the session but rather with the router

var groupid = Session.get('groupID');
Router.go('/groups/:_id',{_id: groupid})

ROUTER:

Router.route('/groups/:_id', function () {
this.render('Group', {
data: function () {
  return GroupsList.findOne({_id: this.params._id});
}
});
});

This will render a route with groupid as the params instead of the actual number


UPDATE:

CLIENT

Router.go('/groups/'+ groupid);

ROUTER

Router.route('/groups/:groupid', function () {
  this.render('Group', {
    data: function () {
      console.log(this.params.groupid)
      console.log(GroupsList.findOne({groupID: this.params.groupid}))
      return GroupsList.findOne({groupID: this.params.groupid});
    }
  });
});

This seems to get the route to work but it wont render the groupname in the template


Solution

  • From the Iron Router Guide:

    Now that we're using named routes in Router.go you can also pass a
    parameters object, query and hash fragment options.
    
    Router.go('post.show', {_id: 1}, {query: 'q=s', hash: 'hashFrag'});
    

    However when you call Router.go, you are not passing a route name, but a url.

    Try this:

    Router.go('/groups/' + groupid);
    
    Router.route('/groups/:_id', function () {
      this.render('Group', {
        data: function () {
          return GroupsList.findOne({groupid: this.params._id});
        }
      });
    });