【问题标题】:Get current user email meteor获取当前用户邮箱流星
【发布时间】:2017-05-26 06:05:06
【问题描述】:

在 Meteor 中获取当前用户的电子邮件有点困难。

发布.js

Meteor.publish('allUsers', function(){
if(Roles.userIsInRole(this.userId, 'admin')) {
return Meteor.users.find({});   
    }
});

Meteor.publish('myMail', function(){ {
    return Meteor.user().emails[0].address; 
    }
});

profile.html

<template name="Profile">
    <h1> My Profile </h1>
    {{#if currentUser}}
<p>{{currentUser.profile.firstName}}</p> <p>{{currentUser.roles}}</p>
<p>{{currentUser.userEmail}}</p>
{{/if}}
</template> 

profile.js

Template.Profile.helpers({
    users: function() {
        return Meteor.users.find();
    },
    userEmail: function() {
        return Meteor.user().emails[0].address;
        }
});

名字和 ._id 显示正常,遗憾的是电子邮件地址没有。有人有小费吗?谢谢!

【问题讨论】:

    标签: meteor profile


    【解决方案1】:

    您的'myMail 发布既多余又不正确。您应该返回一个游标(或一个游标数组),或者观察一个游标并自己发送处理发布生命周期(一个相当高级的功能,与您的问题无关)。你正在使用它Meteor.methods,无论如何你不应该在出版物中真正使用Meteor.user()

    这是多余的,因为 Meteor 的帐户包会自动发布当前用户的 emails 字段。

    在您的模板中,您将userEmail 视为当前用户的属性,而不是将其称为助手。

    我建议使用警卫并确保用户确实有一个电子邮件地址,如下所示:

    JS:

    Template.Profile.helpers({
      users: function() {
        return Meteor.users.find();
      },
      userEmail: function(user) {
        if (user.emails && user.emails.length > 0) {
          return user.emails[0].address;
        }
        return 'no email';
      }
    });
    

    HTML:

    <template name="Profile">
        <h1> My Profile </h1>
        {{#if currentUser}}
        <p>{{currentUser.profile.firstName}}</p> <p>{{currentUser.roles}}</p>
        <p>{{userEmail currentUser}}</p>
        {{/if}}
    </template>
    

    我还强烈建议不要在 'allUsers' 发布中发布所有字段,因为它会暴露在几乎任何情况下都不应离开服务器的敏感数据(例如密码数据)。

    【讨论】:

      猜你喜欢
      • 2014-12-18
      • 1970-01-01
      • 1970-01-01
      • 2011-06-03
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 2019-12-02
      相关资源
      最近更新 更多