Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

meteor

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.

like image 636
akaHeimdall Avatar asked Mar 27 '14 01:03

akaHeimdall


People also ask

What is the meteor user () function for?

users for more on the fields used in user documents. On the server, this will fetch the record from the database. To improve the latency of a method that uses the user document multiple times, save the returned record to a variable instead of re-calling Meteor. user() .

What is the name of the package that provides basic user accounts functionality?

`accounts-base` This package is the core of Meteor's developer-facing user accounts functionality.


1 Answers

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}}
like image 178
chaosbohne Avatar answered Oct 21 '22 17:10

chaosbohne