2017-05-25 55 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的敏感數據事實(例如密碼數據)。

相關問題