2017-05-25 88 views
0

我有点难以获得流星当前用户的电子邮件。获取当前用户电子邮件流星

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显示效果细腻,EMAILADDRESS遗憾的是不。有没有人有小费?谢谢!

回答

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'发布中的所有字段,因为它会暴露不应将服务器留在几乎任何cir的敏感数据事实(例如密码数据)。

相关问题