NodeJS sendGrid邮件不支持导入

hlswsv35  于 2023-05-06  发布在  Node.js
关注(0)|答案(2)|浏览(168)
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)。当我把那条线拿掉。它给出错误,因为变量未定义(阅读发送)。

ux6nzvsh

ux6nzvsh1#

尝试导入MailService属性:

import { MailService } from "@sendgrid/mail";

const sendgridClient = new MailService();

sendgridClient.setApiKey(process.env.SENDGRID_API_KEY || "");
ngynwnxp

ngynwnxp2#

在import语句的末尾有一个右括号。我想你改信的时候忘了把它取下来。请将其删除并重试。

相关问题