我收到“未处理拒绝错误:发送后无法设置标头”。

时间:2018-04-03 10:47:02

标签: node.js express nodemailer

我正在尝试发送电子邮件验证链接,但我收到以下错误

Error: Can 't set headers after they are sent. 
    at validateHeader(_http_outgoing.js: 491: 11)
    at ServerResponse.setHeader(_http_outgoing.js: 498: 3)
    at ServerResponse.header(C: \Users\ fenil.k.shah\ GDPR\ gdpr - portal - server\ node_modules\ express\ lib\ response.js: 730: 10)
    at ServerResponse.send(C: \Users\ fenil.k.shah\ GDPR\ gdpr - portal - server\ node_modules\ express\ lib\ response.js: 170: 12)
    at ServerResponse.json(C: \Users\ fenil.k.shah\ GDPR\ gdpr - portal - server\ node_modules\ express\ lib\ response.js: 256: 15)
    at ServerResponse.send(C: \Users\ fenil.k.shah\ GDPR\ gdpr - portal - server\ node_modules\ express\ lib\ response.js: 158: 21)
    at C: \Users\ fenil.k.shah\ GDPR\ gdpr - portal - server\ controllers\ user.controller.js: 110: 45
    at tryCatcher(C: \Users\ fenil.k.shah\ GDPR\ gdpr - portal - server\ node_modules\ bluebird\ js\ release\ util.js: 16: 23)
    at Promise._settlePromiseFromHandler(C: \Users\ fenil.k.shah\ GDPR\ gdpr - portal - server\ node_modules\ bluebird\ js\ release\ promise.js: 512: 31)
    at Promise._settlePromise(C: \Users\ fenil.k.shah\ GDPR\ gdpr - portal - server\ node_modules\ bluebird\ js\ release\ promise.js: 569: 18)
    at Promise._settlePromise0(C: \Users\ fenil.k.shah\ GDPR\ gdpr - portal - server\ node_modules\ bluebird\ js\ release\ promise.js: 614: 10)
    at Promise._settlePromises(C: \Users\ fenil.k.shah\ GDPR\ gdpr - portal - server\ node_modules\ bluebird\ js\ release\ promise.js: 693: 18)
    at Async._drainQueue(C: \Users\ fenil.k.shah\ GDPR\ gdpr - portal - server\ node_modules\ bluebird\ js\ release\ async.js: 133: 16)
    at Async._drainQueues(C: \Users\ fenil.k.shah\ GDPR\ gdpr - portal - server\ node_modules\ bluebird\ js\ release\ async.js: 143: 10)
    at Immediate.Async.drainQueues(C: \Users\ fenil.k.shah\ GDPR\ gdpr - portal - server\ node_modules\ bluebird\ js\ release\ async.js: 17: 14)
    at Immediate.(C: \Users\ fenil.k.shah\ GDPR\ gdpr - portal - server\ node_modules\ async - listener\ glue.js: 188: 31)

以下是我写的代码

function createUser(group) {

    // set user object to userParam without the cleartext password
    var user = _.omit(req.body, ['password']);

    // add hashed password to user object
    user.password = bcrypt.hashSync(req.body.password, 10);
    user.user_group = group._id;
    user.isActive = false;
    var userlogin = new models.User(user);
    userlogin.save().then(function (user) {
        console.log("User"+user)
        var mailToken ="hgSgajAhagtahaFg";
        var mobileOTP =Math.floor(Math.random() * 10000) + 9999 ;
        var token = new userToken.Token({ _userId: user._id, token: mailToken , otp: mobileOTP });
        token.save().then(function (err) {
            console.log("Token ="+token);
            if (err) {
                console.log("Error"+err)
                 return res.status(500).send({ msg: err.message });
                }

            // Send the email
           else{ 
            var transporter = nodemailer.createTransport(smtpTransport({
              service: "Gmail",
              auth: {
                  user: '**********@gmail.com',
                  pass: "********"
              }
            }));
            console.log("Transporter"+transporter);

            var mailOptions = { from: '*******@gmail.com', to: user.email, subject: 'Account Verification Token', text: 'Hello,\n\n' + 'Please verify your account by clicking the link: \nhttp:\/\/' + req.headers.host + '\/confirmation\/' + token.token + '.\n' };
            transporter.sendMail(mailOptions, function (err) {
                if (err) { return res.status(500).send({ msg: err.message }); }
                res.status(200).send('A verification email has been sent to ' + user.email + '.');
            });
            console.log('mail options '+mailOptions)
        }
        });

        return res.send(user);
    }).catch(function (err) {
        console.log(err)
        res.status(500).send(err);
    });
 }
}

2 个答案:

答案 0 :(得分:1)

这是因为当您发送电子邮件时,您将返回状态为200的响应,然后再次执行res.send。响应只能发送一次。

答案 1 :(得分:0)

您只能为单个HTTP请求发送一个响应。

在你的情况下,你发送两次

  1. res.status(200).send('A verification email has been sent to ' + user.email + '.'); - 您在发送邮件后发送回复
  2. return res.send(user); - 功能结束时
  3. 所以基本上邮件发送需要时间并且res.send(user);将在res.status(200).send('A verification email has been sent to ' + user.email + '.');之前执行,因此评论后面的行将解决您的问题。您可以输入好的日志来确认邮件。