获取当前用户的电子邮件流星

时间:2017-05-25 19:56:27

标签: meteor profile

我在Meteor中收到当前用户的电子邮件时遇到了一些困难。

publish.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;
        }
});

Firstname和._id显示正常,但不幸的是,emailaddress没有。有人有小费吗?谢谢!

1 个答案:

答案 0 :(得分:1)

您的'myMail出版物既冗余又不正确。您应该返回游标(或游标数组),或观察游标并自行发送处理发布生命周期(一个相当高级的功能,与您的问题无关)。你正在使用a-la 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'出版物中的所有字段,因为它会暴露几乎不会在任何情况下离开服务器的敏感数据(例如,密码数据)。