javascriptmongodbmeteormeteor-accountsmeteor-helper

Using the MeteorJS stack, how do I retrieve a user's email with their userId from a post they made


MeteorJS newbie here. I have a collection of "posts" which has fields: title, createdAt, body, userId. Although I only have two users, there seems to be a different userId for each post. I basically just want to display the blog posts, along with their respective user's e-mails. My current implementation is as follows and it is displaying the email of the current user, not the owner of the blog post's email:

"click .main-feed-post" : function(event) {
...
document.getElementById('post-view-email').innerHTML = Meteor.users.findOne({_id: this.userId}).emails[0].address;
...
}

That is currently outputting only the logged-in user's email address. The previous is for the viewing individual blog posts. In addition, I have a main feed where I list all the blog posts and their respective owner's emails:

    {{#each posts}}
        <li class="main-feed-post">
            {{title}}
            <div class="main-feed-post-data">
            <label>BY</label> {{getUserEmail}} <label>AT</label>                                         {{formattedDate}}
    </div>
   </li>

...

getUserEmail : function() {
        return Meteor.users.findOne({_id: this.userId}).emails[0].address;

}
   {{/each}}

This is outputting nothing, unless it is the current user's email. Ideally, I would add a username field to the user object and display that instead of the userId or email. I'm not sure how to implement this using the accounts-ui and accounts-password packages. Any help is welcome! Thanks in advance!


Solution

  • Inside Server:

       Meteor.publish("allUsers", function () {
        return Meteor.users.find({});
    });
    Meteor.publish("allUserData", function () {
        return Meteor.users.find({}, {fields: {"emails.address": 1}});
    });
    

    Inside Client:

    Meteor.subscribe("allUsers");
    Meteor.subscribe("allUserData");