SendGrid发送后无法设置标头

时间:2018-03-23 16:05:13

标签: node.js sendgrid

我收到错误,我拒绝承诺:

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: Can't set headers after they are sent.

发送电子邮件后我是否应该返回JSON,或者解决此问题的最佳方法是允许我从页面发送多封电子邮件。

import * as httpStatus from 'http-status';
import config from '../config/config';

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

function sendemail(req, res) {
  sgMail.setApiKey(config.sendgridKey);

  const msg = {
    to: req.body.to,
    from: req.body.from,
    subject: req.body.subject,
    text: req.body.content,
    html: '<strong>' + req.body.content + '</strong>',
  };

  sgMail.send(msg).then(result => {
    return res.sendStatus(httpStatus.OK).json({ success: true });
  }, err => {
    return res.sendStatus(httpStatus.INTERNAL_SERVER_ERROR).json({ success: false });
  });
}

export default {
  sendemail
};

1 个答案:

答案 0 :(得分:0)

您无法使用<ul> <li *ngFor="let category of categories"> {{ category.name }} </li> </ul> 及之后使用res.sendStatus

res.json

您需要使用res.sendStatus(xxx).json(xxx) => dont use

相关问题