【问题标题】:Bad Request Error Sending from Nodejs - Raw Message Missing header info从 Nodejs 发送的错误请求错误 - 原始消息缺少标头信息
【发布时间】:2021-10-30 14:03:43
【问题描述】:

我正在尝试在 vercel 主机上使用 nodejs 发送电子邮件。

我使用 sendgrid/mail api 收到了错误的请求。看起来好像它没有在对 Sendgrid 的请求中创建标头信息。

当我转储消息时,它只是我发送的 json 信息吗?

这是我正在使用的代码。

const sgMail = require('@sendgrid/mail')

const SENDGRID_API_KEY = process.env.SENDGRID_API_KEY;

export default function (req, res) {

    const mailData = {
        from: 'dhook@fullonconsulting.com',
        to: req.body.email,
        subject: 'Message to Full On Consulting',
        text: req.body.message,
        html: '<div>'+req.body.message+'</div>'
    }
    const {
      classes: {
        Mail,
      },
    } = require('@sendgrid/helpers');
    const mail = Mail.create(mailData);
    const body = mail.toJSON();
    console.log("======================================== ");
    console.log("RAW BODY: " + JSON.stringify(body));
    console.log("======================================== ");

    sendMail(mailData)
    .then((result) => {
            console.log('Email sent...', result);
            res.status(200).json({ status: 'SUCCESS' })
        })
    .catch((error) => console.log('Error ... ' + error.message));
}

async function sendMail(mailData) {
  try {

    var promise = new Promise( (resolve, reject) => {

      sgMail.setApiKey(SENDGRID_API_KEY)

      const msg = {
        to: mailData.to, // Change to your recipient
        from: mailData.from, // Change to your verified sender
        subject: mailData.subject,
        text: mailData.text,
        html: mailData.html,
      }
      console.log("API KEY: " + SENDGRID_API_KEY);
      console.log(JSON.stringify(msg));

      sgMail
        .send(msg)
        .then(() => {
          console.log('Email sent');
        })
        .catch((error) => {
          console.error(error);
          console.log('RECEIVED ERROR')
        })
    });

     //promise.then( result => {
     // console.log("PRomise Success ...");
     //}, function(error) {
     // console.log("Promise Failure...");
     //});

  } catch (error) {
        console.log("CATCH ERROR: " + error)
        return error;
  }
}

这是错误

ResponseError: Bad Request
    at node_modules/@sendgrid/client/src/classes/client.js:146:29
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  code: 400,
  response: {
    headers: {
      server: 'nginx',
      date: 'Sat, 30 Oct 2021 13:51:20 GMT',
      'content-type': 'application/json',
      'content-length': '219',
      connection: 'close',
      'access-control-allow-origin': 'https://sendgrid.api-docs.io',
      'access-control-allow-methods': 'POST',
      'access-control-allow-headers': 'Authorization, Content-Type, On-behalf-of, x-sg-elas-acl',
      'access-control-max-age': '600',
      'x-no-cors-reason': 'https://sendgrid.com/docs/Classroom/Basics/API/cors.html',
      'strict-transport-security': 'max-age=600; includeSubDomains'
    },
    body: { errors: [Array] }
  }
}

【问题讨论】:

  • 如果您在catch 块中使用console.log(error.response.body);,您会得到什么?我只能看到它说errors: [Array],但查看该数组中的内容将是最有用的。

标签: node.js next.js sendgrid sendgrid-api-v3


【解决方案1】:

我没有从导致问题的前端表单正确传递信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-17
    • 2022-01-19
    • 1970-01-01
    • 2017-08-18
    • 1970-01-01
    相关资源
    最近更新 更多