How do I retrieve a list of users who have "liked" this post from a collection and display it in a template?
Collections:
likes: {
"_id": 1234,
"userId": "1dsaf8sd2",
"postId": "123445"
}, {
"_id": 1235,
"userId": "23f4g4e4",
"postId": "123445"
}
users: {
"_id": 1 dsaf8sd2,
"profile": {
"name": "Bob",
"details": "Cool sentence about Bob."
}
}, {
"_id": 23 f4g4e4,
"profile": {
"name": "Sam",
"details": "Cool sentence about Sam."
}
}
Publish:
Meteor.publish('likes', function(postSlug) {
check(postSlug, Object);
// find the post with matching slug and get its id
var postId = Posts.findOne({
slug: postSlug
}, {
_id: 1
});
// find all users who liked this post
var data = Likes.find({
postId: postId
}).forEach(function(doc) {
return Meteor.users.find({
_id: doc.userId
});
});
if (data) {
return data;
}
return this.ready();
});
Helper for template:
Template.listLikers.helpers({
likers: function(){
return this;
}
});
Display each user who liked this post in the template:
{{#each likers}}
<h1>{{name}}</h1>
<p>{{details}}</p>
<a href="/profile/{{_id}}">See Full Profile</a>
{{/each}}
I'm unsure if my collections are structured correctly or if there is an issue with my forEach() function.