如何在视图中显示Meteor.user()配置文件数据

时间:2014-03-27 01:19:05

标签: meteor

我想这必须是一个基本问题,但是我一直在努力解决这个问题太久了。我对Meteor来说比较新。

我查看过Meteor.user()(http://docs.meteor.com/#meteor_users)的文档,可以看到如何向user.profile添加其他信息。即,

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

});

如何在视图模板中显示配置文件信息?我可以通过视图和Web控制台(Meteor.user())访问用户对象,但是我无法访问对象详细信息。

我最初的想法是我可以在我的车把模板中加载以下内容,但它们不起作用:

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

非常感谢任何帮助。

4 个答案:

答案 0 :(得分:28)

您的插页是正确的。

但要显示像名字这样的信息,你必须提供辅助功能。

你的html模板:

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

你的js-code:

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

您可以另外使用{{currentUser}}帮助程序包装用户模板,以确保有用户。

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

答案 1 :(得分:15)

如果您不想为嵌套在{{currentUser}}中的对象的不同属性定义代理帮助器,则可以在模板中完全执行以下操作:

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

已更新以反映评论建议。

答案 2 :(得分:6)

在您的模板中,您希望使用{{currentUser}}代替{{Meteor.user()}}

Docs

答案 3 :(得分:0)

试试这种方式

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

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