meteoraccounts

How can I get other users' profiles details in meteor


I have got problem accessing the user profile details of the users other then the current user.

The goal is to display a little footer under a each of the posts in the kind of blog entries list . Footer should consist of the post and author details (like date, username etc.).

Blog entry is identified by authors' _id but the point is that I can not access

Meteor.users.find({_id : authorId});

Resulting cursor seems to be the same as Meteor.user (not 'users') and consists of one only document, and is valid for the current user ID only. For others, like authors ID, I can only get an empty collection.

The question is, if is there any way, other then next Meteor.users subscription to get authors profile (like username profile.nick etc) ???


Solution

  • Update: You can Publish Composite package if you want to get blog entry and user details in a single subscription. See the following sample code and edit as per your collection schemas,

    Meteor.publishComposite('blogEntries', function (blogEntryIds) {
        return [{
            find: function() {
                return BlogEntries.find({ courseId: { $in: blogEntryIds }});
                // you can also do -> return BlogEntries.find();
                // or -> return BlogEntries.find({ courseId: blogEntryId });
            },
            children: [{
                find: function(blogEntry) {
                    return Meteor.users.find({ 
                        id: blogEntry.authorId 
                    }, { 
                       fields: { 
                            "profile": 1,
                            "emails": 1
                       } 
                    });
                }
            }}
        }]
    });
    

    End of update

    You need to publish Meteor.users from the server to be able to use it on client. accounts package will publish current user, that's why you are only seeing current user's information.

    In a file in server folder or in Meteor.isServer if block do something like this

    //authorIds = ["authorId1", "authorId2];
    Meteor.publish('authors', function (authorIds) {
        return Meteor.users.find({ _id : { $in: authorIds }});
    });
    

    or

    Meteor.publish('author', function (authorId) {
        return Meteor.users.find({ _id : authorId });
    });
    

    Then on client side subscribe to this publication, in template's onCreated function, with something like this

    Meteor.subscribe('author', authorId); //or Meteor.subscribe('author', authorIds);
    

    or

    template.subscribe('author', authorId); //or template.subscribe('author', authorIds);