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

标题已经发送但不确定原因

运维笔记admin7浏览0评论

标题已经发送但不确定原因

标题已经发送但不确定原因

我知道这已经回答了1000次,但我不能为我的生活找出为什么它试图不止一次发送标题。所以,如果您要标记为重复,请解释为什么它是重复的以及我出错的地方。没有一些解释,链接很少有用。

好吧我的问题。我有一个简单的确认路由运行中间件/控制器当我尝试再次重新发送确认令牌时复制用户点击确认链接第二次它告诉我我旁边注意到的行导致我重新发送标题。

用户的令牌仍在我的数据库中(计划更改此设置),但无关紧要,因为似乎导致错误的行只是检查用户配置文件以查看它是否已经过验证。

router.post('/confirmation', 
  user.confirmationPost);


exports.confirmationPost = function (req, res, next) {
    // Find a matching token
    Token.findOne({ token: req.body.token }, function (err, token) {
        if (!token) return res.status(400).send({ type: 'not-verified', message: 'We were unable to find a valid token. Your token my have expired.' });

        // If we found a token, find a matching user
        User.findOne({ _id: token._userId }, function (err, user) {
            if (!user) return res.status(400).send({ message: 'We were unable to find a user for this token.' });
            if (user.isVerified) return res.status(400).send({ message: 'This user has already been verified.' }); // THIS LINE CAUSES THE ERROR

            // Verify and save the user
            user.isVerified = true;
            user.save(function (err) {
                if (err) { return res.status(500).send({ message: err.message }); }
                res.redirect(`${config.siteURL}/dash`);
            });
        });
    });
    next();
};

错误信息

Error: Can't set headers after they are sent. 
回答如下:

弄清楚我的问题,问题是next()接近结束,它是在res.send / json之后调用的,它正试图再次传递/设置标题。

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论