Home > database >  Cannot set headers after they are sent to the client using express-session
Cannot set headers after they are sent to the client using express-session

Time:06-28

    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. Code from official site

CodePudding user response:

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.

CodePudding user response:

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;
}
  • Related