如何通过客户端或服务器上的第三方服务获取登录用户的电子邮件地址?

问题描述 投票:0回答:2

这为您提供了通过帐户密码登录用户的电子邮件地址。

Meteor.user().emails[0].address

用户使用第三方服务登录时,如何获取客户端/服务器端的邮箱地址?例如。脸书,谷歌。

我想从客户端调用以下方法

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: "[email protected]",
            subject: "some subject",
            text: "sometext"
        });
    }
});

我明白了

TypeError: Cannot read property '0' of undefined

meteor meteor-accounts
2个回答
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: "[email protected]",
                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: "[email protected]",
                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: "[email protected]",
                subject: "something",
                text: "something"
            });
        }
    }
});

我认为它只是假设它可以向空字符串/null/undefined 发送电子邮件。我试图在

if(!userEmail)
上抛出 Meteor.Error 没有运气。如果有人能让这段代码更整洁,我将不胜感激。


0
投票
Meteor.call('sendEmail', function(error) {
  if (error) {
    if (error.error === 'user-not-found') {
      console.log('Error: User not found');
    } else if (error.error === 'email-not-found') {
      console.log('Error: User email not found');
    } else {
      console.log('Error sending email:', error);
    }
  } else {
    console.log('Email sent successfully');
  }
});

它对你的情况就像一个魅力

© www.soinside.com 2019 - 2024. All rights reserved.