metpb
metpb

Reputation: 533

How do I use the current user's username as a Router parameter in iron:router

I have a 'profile' template where I will display user related stuffs. So I wanna make a route for the template, but in the 'path' I want to dynamically insert the current user's username. Just the way we dynamically change the url with regard to post's id and everything. Here's the router code block as of now.

Router.map(function() {

    this.route('profile', {
        path: '/profile', //here instead of 'profile' I wanna dynamically insert the current user's username.
    });
});

By the way, I was able to load the user related data's to the said template. I tried loading the username(/username) to the route path in a trial and error way, but in vain. :(
I guess I'm not very good with Iron Router after all. Please help.

Upvotes: 4

Views: 1523

Answers (4)

Isaac Gregson
Isaac Gregson

Reputation: 2075

I too was struggling with this one for a while... then I came across this SO answer. In my case, I was doing everything right except for failing to pass the username along with the template pathFor link helper.

For some reason, when using :_id in iron router routes, there's no need to reference it in the pathFor helper. This was the source of my confusion, perhaps others' as well.

Here is sample code of using the username in a path for iron router:

router.js

this.route('/:username', {
    name: "dashboard",
    waitOn: function() {
      return Meteor.subscribe("allUserData");
    },
    data: function() {
      return Meteor.users.findOne();
    }
  });

publications.js

Meteor.publish("allUserData", function() {
  if (this.userId) {
    return Meteor.users.find(this.userId)
  } else {
    this.ready()
  }
})

page.html

<a href="{{pathFor 'dashboard'  username=username}}"> 
  User Dashboard
</a>

Again, at least in my particular case, I was missing the above username=username.

Upvotes: 1

Seth Malaki
Seth Malaki

Reputation: 4486

Don't forget the waitOn property on routes. Most of the time it's just the timing that's off, creating a publication for this is the best way to get rid of that issue..

Server side, publications.js:

Meteor.publish('me', function() {
   if(!this.userId) return false;
   else return Meteor.users.find({_id: this.userId});
});

In one of your Router.map() routes:

this.route('me', {
    template: 'profile',
    notFoundTemplate: 'profile_not_found', 
    path: '/profile',
    waitOn: function() {
        return Meteor.subscribe("me");
    },
    data: function() {
        return Meteor.user();
    }
});

Don't forget these configuration bits as well:

// Router config.. pretty self explanatory
Router.configure({
    layoutTemplate: 'main',
    notFoundTemplate: 'not_found',
    loadingTemplate: 'loading'
});
// handle the loading screen
Router.onBeforeAction('loading');
// make sure you define routes here that rely on data to throw back
//   404/not found equivalent pages. e.g. no search results found, 
//   or in this case profile not found
Router.onBeforeAction('dataNotFound', {only: ['profile']});

and you can use the profile template:

  <template name="profile">
      Current user Id: {{_id}}
  </template>

  <template name="profile_not_found">
      Profile not found. Are you logged in?
  </template>

Upvotes: 0

Tomas Hromnik
Tomas Hromnik

Reputation: 2200

Use router parameters:

Router.map(function() {
  this.route('profile', {
    path: '/:_username', //dynamic parameter username
    data: function() {
      //here you will get the username parameter
      var username = this.params.username;
      return {
        user: Meteor.users.find({ username: username }) //you can use user object in template
      };
    }
  });
});

Upvotes: 0

Have you tried this?

this.route('profile', {
    path: '/:username',
    data: function() { return Meteor.user().username; }
});

Upvotes: 0

Related Questions