最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

发送后无法设置标题:Raspbian webserver上的Sendgrid邮件响应

运维笔记admin13浏览0评论

发送后无法设置标题:Raspbian webserver上的Sendgrid邮件响应

发送后无法设置标题:Raspbian webserver上的Sendgrid邮件响应

在发送带有sendgrid的电子邮件后,我遇到了渲染页面的问题。问题很可能出现在下面的代码段中:

  sgMail.send(msg, function(err, json) {
if (err) {
  return res.render('contactResponse', 
    {title: 'An error with sending the email has occured. Please try again later or contact me via LinkedIn'});
}

res.render('contactResponse', 
{title: 'Thank you for your email. I will respond as soon as possible.'});

});

现在在localhost中,这段代码完全正常。它呈现“感谢您的电子邮件消息并发送给我并发送电子邮件。然后我将其上传到我的Raspberry服务器并启动它。现在它在控制台中出现以下错误:错误:500 - 无法在它们之后设置标题发送。 - / contact - POST它显示屏幕显示“发送电子邮件时出错”。

现在从我读过的其他帖子中,这可能是res.end在res.render或res.send等方法中被调用2次的问题。我认为这可能是一个问题,我发送邮件,在我渲染响应屏幕后。我不确定是否是这种情况,它适用于localhost所以我不完全理解为什么它在我的服务器上行为奇怪。

现在,我愿意接受有关如何使用“谢谢”或“发生错误”来呈现响应的建议,而不会出现此错误。也可能是其他东西是我的代码中的罪魁祸首。有关完整代码,请参阅this pastebin。

回答如下:

你的router.post('/', function(req, res, next) {...}函数中有多个地方可以调用res.render(),但是然后执行其余的函数继续执行,因此可以再次调用res.render(),这是导致“发送后无法发送标题”类错误的原因。每个请求只能发送一个响应。之后,Express会识别出这是一个错误并记录该特定错误消息。

所以,基本上你正在使用的代码风格:

if (some condition) {
    return res.render()
} 

// more code here

只有在if()进入某些异步回调之前才能工作。一旦它,return只从异步回调返回,并不会停止执行外部函数的其余部分。相反,您需要在异步回调中嵌套if语句。

以下是您遇到问题的一个案例:

request(verificationUrl, function(error, response, body) {
    body = JSON.parse(body);
    // Success will be true or false depending upon captcha validation.
    if (body.success !== undefined && !body.success) {
        // this return ONLY returns from the request() callback, not from the
        // request handler, thus the rest of the request handler continues to
        // execute
        return res.render('contact', {
            title: "Let's talk!",
            name: req.body.name,
            email: req.body.email,
            subject: req.body.subject,
            content: req.body.content,
            errs: [{
                msg: "Failed captcha verification, try again."
            }]
        });
    }
});

// more code continues here, will allow multiple calls to res.render()
// to happen if the above one got called

return res.sender()位于request()回调中,所以return没有从app.post()返回,它只从request()回调返回,然后执行功能体继续。你可能需要做的是使用更多的if/else和更多的嵌套,这样当你的代码落入一个执行分支时,它就不会继续执行其他分支。

例如,解决问题的这种特殊情况的一种方法是将一个else放在request()处理程序中,如下所示:

request(verificationUrl, function(error, response, body) {
    body = JSON.parse(body);
    // Success will be true or false depending upon captcha validation.
    if (body.success !== undefined && !body.success) {
        return res.render('contact', {
            title: "Let's talk!",
            name: req.body.name,
            email: req.body.email,
            subject: req.body.subject,
            content: req.body.content,
            errs: [{
                msg: "Failed captcha verification, try again."
            }]
        });
    } else {
        // rest of function body continues here
    }
});

// no more function body code here

现在在localhost中,这段代码完全正常

逻辑显然是错误的。如果它能够工作,那只是因为你非常幸运,无论是异步计时,还是通过你的if逻辑的特定流程都会变得幸运。必须修复逻辑以可靠且重复地工作。

发布评论

评论列表(0)

  1. 暂无评论