Sendgrid Node.js Error: Converting circular structure to JSON
Sendgrid Node.js Error: Converting circular structure to JSON
我正在使用 Sendgrid 通过 Firebase Cloud Functions 使用 Nodejs 发送交易电子邮件。
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" });
根据doc:
对于您发送的文件的 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' });
我正在使用 Sendgrid 通过 Firebase Cloud Functions 使用 Nodejs 发送交易电子邮件。
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" });
根据doc:
对于您发送的文件的 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' });