Reputation: 23
So I'm creating a user profile pages, and have gotten all the data for the current user working fine. the issue I'm having is that the current user can't see other users data.
I've looked at everything I can find on the web but haven't find an answer that has helped me, just many different ways of doing one thing.
my publish
Meteor.publish('allUsers', function(user){
return Meteor.users.find({_id: this.userId},{
fields: {
profile: 1,
emails:1,
username: 1
}
});
});
my router
Router.route('/profile/:_id', {
name: 'profile',
waitOn: function(){
return Meteor.subscribe('allUsers')},
data: function(){
var user = this.params._id;
return Meteor.users.findOne({_id: user});
},
});
profile template
<template name='profile'>
Username: {{username}}<br>
Email: {{email}}
{{log}}
{{#with profile}}
Full Name: {{name}}
{{log}}
{{/with}}
</template>
helper
Template.profile.helpers({
name: function(){
return this.name;
},
username: function(){
return this.username;
},
email: function(){
return this.emails[0].address;
},
log: function(){
console.log(this);
}
});
and this is how I'm linking to other users
<a href="/profile/{{owner}}">{{username}}</a>
owner being the users _id:
the log output for the current user is showing the data though the log output of the other user is an empty object. though it is grabbing the right _id.
Upvotes: 1
Views: 35
Reputation: 4101
In your allUsers
publication, you find the user with {_id: this.userId}
. this.userId
is the ID of the user who is subscribing. That means you're publishing the user's own data, and not the data of any other users. If you wanted to publish all of the users, you'll want to use .find({}, {fields: ...})
instead.
For applications with more than a few users, publishing all the users is not a good idea, so instead you can publish just one user:
Meteor.publish("userById", function (requestedUserId) {
return Meteor.users.find({_id: requestedUserId}, {fields: ...});
});
Then when you subscribe in the router, pass in the user id:
Meteor.subscribe("userById", this.params._id);
Upvotes: 1