Search code examples
meteorpublish-subscribemeteor-helper

Meteor: Join by userId but display username, publishing user data?


So, I have bunch of collections joined with Meteor.users collection by userId as foreign key. E.g. I have Posts collection and each post has its own page. On a post page I have userId as author and a bunch of comments(from Comments collection) on that post each has postId and userId. UserIds are ugly, instead I would like to display usernames. How would you achieve that? Any smart package that I can use or working example? I don't like the idea of publishing all users data. Thank you.


Solution

  • You don't need to publish all data. In publish method you can use fields specifier in that way:

    Meteor.users.find({}, {fields: {'username': 1}});

    That way you have only _id and username published.

    Then in meteor helper you can have:

    username: function() {
      var user = Meteor.user.findOne(this.userId);
      return user.username || user.profile.name;
    }
    

    if Post is your context