akaHeimdall
akaHeimdall

Reputation: 959

How to display Meteor.user() profile data in the view

I imagine this has to be an elementary issue however I've been struggling through this for too long. I'm relatively new to Meteor.

I've viewed the documentation for the Meteor.user() (http://docs.meteor.com/#meteor_users) and can see how additional information is added to the user.profile. I.e.,

//JS file
Meteor.users.insert({
    username: 'admin',
    profile: {
                first_name: 'Clark',
                last_name: 'Kent'
    },

});

How then do I display the profile information in the view template? I can access the user object via the view and web console (Meteor.user()) however I cannot access the object details.

My initial thoughts were that I could load the following in my handlebar templates but they do not work:

// HTML view
{{Meteor.user().username}}
{{Meteor.user().profile.first_name}}
{{Meteor.user().profile.last_name}}

Any help is greatly appreciated.

Upvotes: 15

Views: 26789

Answers (4)

kumbhani bhavesh
kumbhani bhavesh

Reputation: 2257

try this way

{{#with userDetails}}
   First name:-{{this.first_name}}
   Last name:- {{this.last_name}}
{{/with}}

 //jsSide
 userDetails(){
   return Meteor.user();
 }

Upvotes: 0

kontur
kontur

Reputation: 5220

If you don't want to define a proxy helper for different attributes of objects nested in {{currentUser}}, you can do the following purely in your template:

{{#with currentUser}}
    {{#with profile}}
        {{first_name}}
    {{/with}}
{{/with}}

Updated to reflect comment suggestion.

Upvotes: 15

chaosbohne
chaosbohne

Reputation: 2474

Your insert is correct.

But to show the information like the first name you have to provide a helper function.

Your html-template:

<template name="user">
  <p>{{firstName}}</p>
</template>

Your js-code:

Template.user.helpers({
  firstName: function() {
    return Meteor.user().profile.first_name;
  }
});

You can additionaly wrap the user template with the {{currentUser}} helper to be sure there is a user.

{{#if currentUser}} 
  {{> user}}
{{/if}}

Upvotes: 28

Jordan H.
Jordan H.

Reputation: 457

In your templates, you'll want to use {{currentUser}} instead of {{Meteor.user()}}.

Docs

Upvotes: 7

Related Questions