Meteor with Iron Route PathFor

2019-08-06 16:18发布

问题:

When I click on user's name, I want the link to go to their profile page. I am using Meteor with iron router. Here are the defined routes:

Router.route('/', { name: 'JobsList'}); //homepage with all users

//this is where the user's profile is located and works:
Router.route('/company/:_id/', {
 name: 'CompanyPage',
 data: function() { return Meteor.users.findOne(this.params._id); }
});

//this page shows all users with url that works:
Router.route('/companies/', {name: 'CompaniesList'});

I get the incorrect link when I hover over the user's name on the homepage but I get the correct link when I hover their name on the '/companies/' page. In order to generate the link, I use pathFor "CompanyPage".

Am I missing something that is causing the incorrect url from the homepage? What js or html you need to have a look at? Let me know and I'll edit this post. Thanks.

回答1:

In order to get the correct link, you have to use the following syntax:

{{pathFor 'CompanyPage' _id=userId }}

Where this._id should be the user's id

What is happening:

For routes with appended identifier of some kind, Iron Router looks for the same identifier name in this. It would work if this is set to the correct data context, however in the case of your / route, you don't have a data context defined. On CompanyPage, the data context is defined to the correct user.