const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
to: 'test@example.com',
from: 'test@example.com',
subject: 'Sending with Twilio SendGrid is Fun',
text: 'and easy to do anywhere, even with Node.js',
html: '<strong>and easy to do anywhere, even with Node.js</strong>',
};
sgMail.send(msg);
以上使用需要导入sendgrid。上面的代码工作。
但是由于我使用的是ES6语法,所以我不能使用require,而必须使用import。
import sgMail from '@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
to: 'test@example.com',
from: 'test@example.com',
subject: 'Sending with Twilio SendGrid is Fun',
text: 'and easy to do anywhere, even with Node.js',
html: '<strong>and easy to do anywhere, even with Node.js</strong>',
};
sgMail.send(msg);
所以,我写了这段代码,但它给出了错误,因为变量未定义(阅读setApiKey)。当我把那条线拿掉。它给出错误,因为变量未定义(阅读发送)。
2条答案
按热度按时间ux6nzvsh1#
尝试导入
MailService
属性:ngynwnxp2#
在import语句的末尾有一个右括号。我想你改信的时候忘了把它取下来。请将其删除并重试。