繁体   English   中英

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

[英]Get current user email meteor

我在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;
        }
});

名字和._id可以正常显示,但不幸的是,电子邮件地址不能。 有人给小费吗? 谢谢!

您的'myMail发布既多余'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'出版物中的所有字段,因为它将公开在几乎任何情况下都不应离开服务器的敏感数据(例如密码数据)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM