Search code examples
meteormeteor-blazemeteor-accounts

Meteor: accessing user details on client


I'm trying to access (another) user's details on the client side in meteor. I have a server side method called 'userDetails' that I'm calling from a template helper called 'acc'.

Server method:

'userDetails': function(userId) {
      check(userId, String);
      return Meteor.users.findOne({_id: userId},
                                  {fields: {
                                    "services.facebook.first_name": 1,
                                    "profile.birthday": 1,
                                    "services.facebook.gender": 1,
                                    "profile.location.name": 1
                                  }});
    }

Template helper:

acc: function(_id) {
    Meteor.call('userDetails', _id, function(err, res) {
      if(err) throw error;
      return res;
    });
  }

When I try to access acc.profile.birthday in the template I don't get anything. What could cause this?


Solution

  • Meteor calls are asynchronous calls, that is why your helper is not returning any data.

    Best option here is to either use Session or ReactiveVar or ReactiveDict

    I'll use Session option here

    acc: function(_id) {
      Meteor.call('userDetails', _id, function(err, res) {
        if(err){
    
        }else{
          Session.set('userDetails', res)
        }
    
      });
      return Session.get('userDetails')
    }
    

    In your html you can use this helper like this

    {{#if acc}}
      {{name}}
      ...
    {{else}}
      <p>Information not found</p>
    {{/if}}