onCreated return undefined for url params


#1

I have a url params which is picked up using the onCreated function. But when I reload the browser the params is undefined. How can I pick up the params even with page reload. This is the code:

Template.userPage.onCreated(function () {
    var currentUser = Router.current().params._id;
    Session.set('Follower', Meteor.users.findOne({ _id: currentUser }));
});

#2

In Iron Router Router.current() is reactive and will be null until a route is run. The docs talk more about this here:

If you’re on the client, you can use the Router.current() method. This will reactively return the current instance of a RouteController. Keep in mind this value could be null if no route has run yet.

One way to work around this would be to wrap your Router.current() call in a Tracker.autorun; something like:

Template.userPage.onCreated(function onCreated() {
  this.autorun(() => {
    const currentUser = Router.current().params._id;
    Session.set('Follower', Meteor.users.findOne({ _id: currentUser }));
  });
});

#3

Thanks I gave that a go but this still returns undefined on page reload.