Node js - send grid issue on sending basic email

戏子无情 提交于 2019-12-24 02:13:55

问题


Node js - send grid issue on sending basic email

I've setup my node js server and need help with the setup.

Error "cant set headers"

This is my code:

  var express = require('express');
  var router = express.Router();
  var sendgrid = require('sendgrid')('IHaveAKey');

  router.get('/', function(req, res) {
    res.sendStatus(200);
  });

  router.get('/welcomeEmail/:email/:name', function(req, res) {
    var subject = 'Hello' + req.params.name;

    sendgrid.send({
      to:       'test@hotmail.co.uk',
      from:     'noreply@test.com',
      subject:  'Test',
      text:     'Welcome'
    }, function(err, json) {
      if (err) { return res.send("Error");}    
      return res.send("Sent");
    });

    res.sendStatus(200);
  });

  module.exports = router;

This is my error:


回答1:


You are triying to send response from server 2 times:

1) res.sendStatus(200);

2) res.send("Sent");

Leave one of this line and everything start to be ok



来源:https://stackoverflow.com/questions/34788083/node-js-send-grid-issue-on-sending-basic-email

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!