【问题标题】:How to display Meteor.user() profile data in the view如何在视图中显示 Meteor.user() 配置文件数据
【发布时间】:2017-08-17 17:41:48
【问题描述】:

我想这一定是一个基本问题,但是我已经为此苦苦挣扎了太久。我对 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}}

非常感谢任何帮助。

【问题讨论】:

  • 只要{{currentUser.profile.first_name}} 就可以了。

标签: meteor


【解决方案1】:

您的插入是正确的。

但要显示名字等信息,您必须提供一个辅助函数。

您的 html 模板:

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

你的 js 代码:

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

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

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

【讨论】:

  • 啊。很抱歉回复得太晚了。非常感谢。
【解决方案2】:

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

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

已更新以反映评论建议。

【讨论】:

    【解决方案3】:

    在您的模板中,您需要使用{{currentUser}} 而不是{{Meteor.user()}}

    Docs

    【讨论】:

      【解决方案4】:

      试试这个方法

      {{#with userDetails}}
         First name:-{{this.first_name}}
         Last name:- {{this.last_name}}
      {{/with}}
      
       //jsSide
       userDetails(){
         return Meteor.user();
       }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-24
        • 2020-05-27
        • 1970-01-01
        • 2017-02-15
        • 2017-05-09
        • 1970-01-01
        相关资源
        最近更新 更多