0

I'm getting this error: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client when I'm running my ExpressJS code.

Please help guys, this is my first question on stackoverflow btw and I don't really know how to ask questions :^)

My code:

app.route("/")

.get(function(req,res){
    res.render("home");
})

app.route("/register")
.get(function(req,res){
    res.render("register");
})

.post(function(req,res){

    bcrypt.hash(req.body.password, saltRounds, function(err, hash) {
        const newUser = new User({
            email: req.body.username,
            password: hash
        })
    
        newUser.save(function(err){
            if (err){
                console.log(err);
            } else {
                res.render("secrets");
            }
        });
    });
});

app.route("/login")
.get(function(req,res){
    res.render("login");
})

.post(function(req,res){
    const username = req.body.username;
    const password = req.body.password;

    User.findOne({email: username}, function(err, foundUser){
        if (err){
            console.log(err);
        } else {
            if (foundUser){
                bcrypt.compare(password, foundUser.password, function(err,result){
                    if (result === true){
                        res.render("secrets");
                    }
                });
            }
        }
    });
    
});

app.listen(3000, function(){
    console.log("Server started...");
});

I got this error after I made a POST request in my login page, it was stuck on loading and It wasn't loading the secrets.ejs page. I think the error is in the login route, 'cause I'm getting that error when I'm making a POST request in login route.

Vedansh
  • 19
  • 3
  • 1
    What if result !== true ? – dinesh oz May 02 '22 at 07:27
  • Can't see anything here that would cause that error. You typically get it if you try and call `res.send()` (or other response methods) more than once. You are missing sending a response for your _unhappy paths_ but that would just cause the request to timeout – Phil May 02 '22 at 07:31
  • Phil, let me provide the whole code. – Vedansh May 02 '22 at 07:34
  • Still don't see potential for that error in your `POST /login` route but you do have a problem in `POST /register` where you're trying to save a new user twice and call `res.render()` twice – Phil May 02 '22 at 07:41
  • Oh yes, maybe that's causing the error, I forgot to remove the second one when I pasted it in the bcrypt function. – Vedansh May 02 '22 at 07:44
  • I fixed that but still the same issue, there's no error this time but it's not rendering the `secrets` page. – Vedansh May 02 '22 at 07:49
  • If there's no error, then it's not the same issue, is it. Like I said, you have logic paths that don't render anything or otherwise let Express know that something went wrong. See https://expressjs.com/en/guide/error-handling.html – Phil May 02 '22 at 07:57
  • @Phil Ig the issue is that bcrypt build is failing as we can see in [this](https://cdn.discordapp.com/attachments/743817386792058971/970608075075649616/unknown.png) image. – Vedansh May 02 '22 at 08:50

0 Answers0