2017-07-03 43 views
0

我试图发送一封电子邮件,在Azure上我的节点JS应用程序,并得到这个错误:SendGrid Azure的问题

TypeError: sendgrid.Email is not a constructor 

这里是我的代码。我使用了微软的文档(https://docs.microsoft.com/en-us/azure/store-sendgrid-nodejs-how-to-send-email)。

var sendgrid = require('sendgrid')('SendGrid User ID', 'SendGrid password'); 
function createEmail() { 
    console.log('CREATE EMAIL'); 

    var emailToSend = new sendgrid.Email({ 
     to: [email protected], 
     from: '[email protected]', 
     subject: 'Subject', 
     text: 'some text'; 
    }); 

    sendEmail(emailToSend); 
} 

function sendEmail(email) { 
    console.log('SEND EMAIL'); 
    sendgrid.send(email, function (err, json) { 
     if (err) { 
      return console.error(err); 
     } 
    }); 
} 
+1

看看这个:https://github.com/sendgrid/sendgrid-nodejs/issues/269 - 它看起来像SendGrid节点支持已经有了突破性的改变,因为你链接的MSDN文章被写入。我链接的GitHub线程有一个更新代码示例的链接:https://sendgrid.com/docs/Integrate/Code_Examples/v3_Mail/nodejs.html –

回答

1

正如@大卫坦西提到的,SendGrid团队的补充重大更改,以支持V3的Web API,因为v3.0.0Here是最新版本(v5.1.2)的工作代码示例。

var helper = require('sendgrid').mail; 
var fromEmail = new helper.Email('[email protected]'); 
var toEmail = new helper.Email('[email protected]'); 
var subject = 'Sending with SendGrid is Fun'; 
var content = new helper.Content('text/plain', 'and easy to do anywhere, even with Node.js'); 
var mail = new helper.Mail(fromEmail, subject, toEmail, content); 

var sg = require('sendgrid')(process.env.SENDGRID_API_KEY); 
var request = sg.emptyRequest({ 
    method: 'POST', 
    path: '/v3/mail/send', 
    body: mail.toJSON() 
}); 

sg.API(request, function (error, response) { 
    if (error) { 
    console.log('Error response received'); 
    } 
    console.log(response.statusCode); 
    console.log(response.body); 
    console.log(response.headers); 
}); 

但是,如果你想流畅运行提供的代码,你需要的版本恢复到2.0.0。