0

I want to re-render a jade from node.js to set an errormessage. I get the Error "Can't set headers after they are sent".

This is my Code...

app.post('/login', function (req, res) {
    checkUserAndPassword(req.body.mail, req.body.password, function (status) {
        console.log(status);

        if (status === 1) {
            getInstances(function (instances) {
                res.render('mainSite', {title: "Systemverwaltung - Übersicht", instances: instances});
            });
        } else {
            res.render('index', {title: "Systemverwaltung", error: "Wrong Mail or Password!"});
        }
    });
});

Has anyone an idea?

Nikos Paraskevopoulos
  • 39,514
  • 12
  • 85
  • 90
  • 3
    Possible duplicate of [Error: Can't set headers after they are sent to the client](https://stackoverflow.com/questions/7042340/error-cant-set-headers-after-they-are-sent-to-the-client) – Serge K. Jul 11 '17 at 09:22

1 Answers1

0

By looking at the code that you provided there doesn't seem to be anything wrong. It means that the problem is likely elsewhere.

Possible problems that can cause those symptoms:

  1. checkUserAndPassword calls the callback twice
  2. getInstances calls the callback twice
  3. some middleware already sent the response before your handler

Of course none of those can be the case and it's impossible to know not seeing that code but those would be my hints to start investigating the issue.

rsp
  • 107,747
  • 29
  • 201
  • 177