How to use the user's current username as a router parameter in hardware: router
I have a template 'profile'
where I will display user-related content. So I want to make a route for the template, but in 'path'
I want to dynamically insert the current username. This is how we dynamically change the url regarding the post id and everything. Here's a block of router code for now.
Router.map(function() {
this.route('profile', {
path: '/profile', //here instead of 'profile' I wanna dynamically insert the current user username.
});
});
By the way, I was able to load user related data into the specified template. I tried loading username ( /username
) into route path in a trial and error way, but to no avail. :(
I guess I'm not very good at Iron Router. Please help.
source to share
I've struggled with this for a while too ... then I came across this SO answer . In my case, I was doing everything right, except that it was unable to pass the username along with the link helper pathFor
.
For some reason, when using an :_id
iron router in routes, there is no need to refer to it in the assistant pathFor
. This was the source of my confusion, and possibly others as well.
Here is an example code for using username in path for an 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 above username=username
.
source to share
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
};
}
});
});
source to share
Don't forget property waitOn
on routes. Most of the time it's just time out, making a post for that is the best way to get rid of this problem.
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 routes Router.map()
:
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 config bits too:
// 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>
source to share