使用明上文将标题发送给客户端后无法设置标题

发布于 2025-02-11 08:08:39 字数 1594 浏览 1 评论 0原文

    const emailaddress = req.body.emailaddress;
const password = req.body.password;
if (emailaddress && password) {
    const query = `SELECT * FROM users where email=? AND password=?`;
    connection.query(
        query,
        [emailaddress, password],
        function (err, results, fields) {
            if (err) {
                res.send({
                    code: 500,
                    failed: "Error ocurred",
                });
            }
            if (results.length > 0) {
                req.session.regenerate(function (err) {
                    // if (err) next(err);
                    req.session.loggedin = true;
                    req.session.emailaddress = req.body.emailaddress;
                    req.session.save(function (err) {
                        // if (err) return next(err);
                        res.redirect("/home");
                    });
                });
            } else {
                res.send("Incorrect email address and/or password!");
            }
            res.end();
        }
    );
} else {
    res.send("Please enter username and/or password");
    res.end();
}

我尝试使用上面的代码使用快递会话来存储会话值。但是它给出以下错误。

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

我怎么避免这种情况。我已经尝试了许多解决方案。我正在遵循他们的文档。请帮忙。谢谢。 “ https://www.npmjs.com/package/package/express-session”

    const emailaddress = req.body.emailaddress;
const password = req.body.password;
if (emailaddress && password) {
    const query = `SELECT * FROM users where email=? AND password=?`;
    connection.query(
        query,
        [emailaddress, password],
        function (err, results, fields) {
            if (err) {
                res.send({
                    code: 500,
                    failed: "Error ocurred",
                });
            }
            if (results.length > 0) {
                req.session.regenerate(function (err) {
                    // if (err) next(err);
                    req.session.loggedin = true;
                    req.session.emailaddress = req.body.emailaddress;
                    req.session.save(function (err) {
                        // if (err) return next(err);
                        res.redirect("/home");
                    });
                });
            } else {
                res.send("Incorrect email address and/or password!");
            }
            res.end();
        }
    );
} else {
    res.send("Please enter username and/or password");
    res.end();
}

I tried using the above code using express-session to store session values. but it gives the following error.

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

How can I avoid this. I already tried many solutions. I am following their documentation. Please help. Thank you.
https://www.npmjs.com/package/express-session

Code from official site

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒路情人 2025-02-18 08:08:39

可能的问题:

else block呼叫<代码>发送和在外面的最后一行,否则调用end()
因此,如果它属于其他块,sendend将被称为引起该错误。

您还可以多次呼叫next(err),而不知道这是下一个处理程序,我可以肯定地告诉您,但他们也可能正在调用send。这将属于与上述相同的情况。

Possible problems:

Else block calls send and last line outside if and else calls end().
So if it falls into the Else block, both send and end will be called causing that error.

You also have multiple call to next(err), without knowing that is the next handler, I can tell you for sure, but they might be calling send as well. Which would fall into the same scenario as above.

霞映澄塘 2025-02-18 08:08:39

我看到您回答后的代码进行了一些更改。
让我重试,您不能使用res.sendres.end一起选择一个。另外,您需要确保您仅调用res.send一次。

您的代码应该是这样的:

const emailaddress = req.body.emailaddress;
const password = req.body.password;
if (emailaddress && password) {
    const query = `SELECT * FROM users where email=? AND password=?`;
    connection.query(
        query,
        [emailaddress, password],
        function (err, results, fields) {
            if (err) {
                res.send({
                    code: 500,
                    failed: "Error occurred",
                });
                return; // make sure your function execution stops here, as you already called send();
            }
            if (results.length > 0) {
                req.session.regenerate(function (err) {
                    // if (err) next(err);
                    req.session.loggedin = true;
                    req.session.emailaddress = req.body.emailaddress;
                    req.session.save(function (err) {
                        // if (err) return next(err);
                        res.redirect("/home");
                    });
                });
            } else {
                res.send("Incorrect email address and/or password!");
                return; // same thing, called send() make sure to end the function
            }
            // res.end(); you probably don't need this
        }
    );
} else {
    res.send("Please enter username and/or password");
    // res.end(); you probably don't need this
    return;
}

I see you made some changes in your code after my answer.
Let me try again, you cannot use res.send and res.end together, you need to pick one. Also you need to make sure you are only calling res.send once.

your code should be something like this:

const emailaddress = req.body.emailaddress;
const password = req.body.password;
if (emailaddress && password) {
    const query = `SELECT * FROM users where email=? AND password=?`;
    connection.query(
        query,
        [emailaddress, password],
        function (err, results, fields) {
            if (err) {
                res.send({
                    code: 500,
                    failed: "Error occurred",
                });
                return; // make sure your function execution stops here, as you already called send();
            }
            if (results.length > 0) {
                req.session.regenerate(function (err) {
                    // if (err) next(err);
                    req.session.loggedin = true;
                    req.session.emailaddress = req.body.emailaddress;
                    req.session.save(function (err) {
                        // if (err) return next(err);
                        res.redirect("/home");
                    });
                });
            } else {
                res.send("Incorrect email address and/or password!");
                return; // same thing, called send() make sure to end the function
            }
            // res.end(); you probably don't need this
        }
    );
} else {
    res.send("Please enter username and/or password");
    // res.end(); you probably don't need this
    return;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文