node.js - Sendgrid Node.js 错误:将循环结构转换为 JSON
问题描述
我正在使用 Sendgrid 使用 Nodejs 通过 Firebase Cloud Functions 发送交易电子邮件。
const attachment = file.createReadStream();
const msg = {
to: email,
from: "test@test.com",
subject: "print order",
templateId: "templateid",
dynamic_template_data: {
dashboardurl: `/order#${orderId}`,
downloadurl: orderData.downloadurl
},
attachments: [{
content: attachment,
filename: "order.pdf",
type: "application/pdf",
disposition: "attachment"
}]
};
await sgMail.send(msg);
由于以下错误,电子邮件不会发送:
TypeError: Converting circular structure to JSON
Firebase 存储解决方案
const tempFilePath = path.join(os.tmpdir(), "tmp.pdf");
await file.download({ destination: tempFilePath });
const attachment = fs.readFileSync(tempFilePath, { encoding: "base64" });
解决方案
根据文档:
对于内容,您发送文件的 base64 编码版本。
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
to: 'recipient@example.org',
from: 'sender@example.org',
subject: 'Hello attachment',
html: '<p>Here’s an attachment for you!</p>',
attachments: [
{
content: 'Some base 64 encoded attachment content',
filename: 'some-attachment.txt',
type: 'plain/text',
disposition: 'attachment',
contentId: 'mytext'
},
],
};
因此,在您的情况下,以下转换就足够了:
const attachment = fs.readFileSync('filepath', { encoding: 'base64' });
推荐阅读
- asp.net-core - 如何从控制器传递对象并在 ASP.Net Core 中的视图上使用它
- c - STM32F4 发现的 USART 超时中断
- ubuntu - 由于端口冲突,无法运行 kubectl,但也无法通过在线解决方案修复它们
- sql-server - 在使用 Ola Hallengren 创建备份后删除 Linux SQL Server 2017 的旧数据库备份?
- batch-file - 在 dos4gw 中回显 %errorlevel%
- reactjs - React Apollo Client - 在查询数据进入缓存之前修改它
- google-api - 执行 Batch API 以将多个用户添加到多个组
- sql - 如何在此查询的这一部分中使用标签?
- vb.net - Lotusscript 将 base64 编码的字符串保存到文件 (DLL)
- mysql - 在python中插入多个数据框时没有从mysql获取值