sendGrid 邮件不适用于导入

问题描述 投票:0回答:1
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
  to: '[email protected]',
  from: '[email protected]',
  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: '[email protected]',
  from: '[email protected]',
  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)。当我删除那条线时。它给出错误,因为变量未定义(读取发送)。

node.js sendgrid
1个回答
0
投票

import 语句末尾有一个右括号。我想你在转换时错过了删除它。请将其删除并重试。

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