dpat
dpat

Reputation: 45

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

Upvotes: 2

Views: 484

Answers (2)

dpat
dpat

Reputation: 45

On a side note from the answer just in case anyone else has this issue , I actually figured out there was an issue in the data type of the "this.params._id" , it seems it was coming up as a data type that was not a string or number and therefore could not be successfully used in the findOne method. In the end I just had to parseInt and this was the solution at the end :

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

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

Upvotes: 0

JeremyK
JeremyK

Reputation: 3240

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});
    }
  });
});

Upvotes: 1

Related Questions