【问题标题】:Sendgrid Node.js Error: Converting circular structure to JSONSendgrid Node.js 错误:将循环结构转换为 JSON
【发布时间】:2020-03-23 21:49:23
【问题描述】:

我正在使用 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"   });

【问题讨论】:

    标签: node.js firebase google-cloud-functions sendgrid sendgrid-templates


    【解决方案1】:

    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' });
    

    【讨论】:

    • 使用转换时出现以下错误:TypeError: path must be a string or Buffer
    • file 不是文件对象,file 这里是文件的路径。
    猜你喜欢
    • 2019-01-31
    • 1970-01-01
    • 2018-05-05
    • 2018-09-17
    • 2018-06-08
    • 2021-10-23
    • 1970-01-01
    相关资源
    最近更新 更多