这会通过accounts-password为您登录用户的电子邮件地址。
Meteor.user().emails[0].address
当用户使用第三方服务登录时,如何在客户端/服务器上获取电子邮件地址?例如。 facebook,谷歌。
我想从客户端调用以下方法
Meteor.methods({
sendEmail: function() {
var userEmail;
if(Meteor.user().emails[0].address) {
return userEmail = Meteor.user().emails[0].address;
} else if (Meteor.user().services.google.email) {
return userEmail = Meteor.user().services.google.email;
} else if (Meteor.user().services.facebook.email) {
return userEmail = Meteor.user().services.facebook.email;
}
Email.send({
to: userEmail,
from: "example@gmail.com",
subject: "some subject",
text: "sometext"
});
}
});
我得到TypeError: Cannot read property '0' of undefined
答案 0 :(得分:0)
我不喜欢这样,但它的工作原理
Meteor.methods({
sendEmail: function() {
this.unblock();
var currentUser = Meteor.user();
if (currentUser && currentUser.emails && currentUser.emails[0]
&& currentUser.emails[0].address) {
var userEmail = currentUser.emails[0].address;
Email.send({
to: userEmail,
from: "something@gmail.com",
subject: "something",
text: "something"
});
} else if (currentUser && currentUser.services && currentUser.services.google
&& currentUser.services.google.email) {
var userEmail = currentUser.services.google.email;
Email.send({
to: userEmail,
from: "something@gmail.com",
subject: "something",
text: "something"
});
} else if (currentUser && currentUser.services && currentUser.services.facebook
&& currentUser.services.facebook.email) {
var userEmail = currentUser.services.facebook.email;
Email.send({
to: userEmail,
from: "something@gmail.com",
subject: "something",
text: "something"
});
}
}
});
我认为它只是假设它可以发送一封电子邮件到空字符串/ null / undefined。我试图在if(!userEmail)
上抛出Meteor.Error没有运气。如果有人可以使这个代码更整洁,我会很感激。