-1

I am getting the following exception :

 throw er; // Unhandled 'error' event
      ^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at validateHeader (_http_outgoing.js:503:11)
    at ServerResponse.setHeader (_http_outgoing.js:510:3)
    at ServerResponse.header (/Users/athulmuralidharan/my_documents/MS/MSD/projects/MLL-backEnd/node_modules/express/lib/response.js:730:10)
    at ServerResponse.send (/Users/athulmuralidharan/my_documents/MS/MSD/projects/MLL-backEnd/node_modules/express/lib/response.js:170:12)
    at ServerResponse.json (/Users/athulmuralidharan/my_documents/MS/MSD/projects/MLL-backEnd/node_modules/express/lib/response.js:256:15)
    at /Users/athulmuralidharan/my_documents/MS/MSD/projects/MLL-backEnd/controllers/users.js:56:13
    at model.Query.<anonymous> (/Users/athulmuralidharan/my_documents/MS/MSD/projects/MLL-backEnd/node_modules/mongoose/lib/model.js:3928:16)
    at /Users/athulmuralidharan/my_documents/MS/MSD/projects/MLL-backEnd/node_modules/kareem/index.js:297:21
    at /Users/athulmuralidharan/my_documents/MS/MSD/projects/MLL-backEnd/node_modules/kareem/index.js:135:16
    at process._tickCallback (internal/process/next_tick.js:150:11)

Intention : To verify if the returned object is null Code:

exports.login = function(req, res,next) {

console.log
User.findOne({email: req.body.username,password: req.body.password}, function(err,obj)
{
    if (err)
        res.send(err);

    if (obj == null)
    {
        console.log("null returned");
        res.status(404).send("Oh uh, something went wrong");

    }
    console.log(obj);
    res.json(obj);
}
);
Athul Muralidharan
  • 693
  • 1
  • 10
  • 18

2 Answers2

1

If err is truthy your program will res.send, which sends headers to the client.

if (err)
    res.send(err);

However, you don't stop your program after that, so the next if statement is also run, and there's a chance that obj is equal to null, therefore you've already used res.send(err) but you try to res.status(404):

if (obj == null)
{
    console.log("null returned");
    res.status(404).send("Oh uh, something went wrong");
}

To fix this you just need to stop the program after you use res.send, using an else statement or a return::

if (err) {
    res.send(err);
} else if (obj == null) {
    console.log("null returned");
    res.status(404).send("Oh uh, something went wrong");
}

alternatively

if (err)
    return res.send(err);

if (obj == null) {
    console.log("null returned");
    res.status(404).send("Oh uh, something went wrong");
}
theonlygusti
  • 11,032
  • 11
  • 64
  • 119
0

You really only missed a fact.

User.findOne({email: req.body.username,password: req.body.password}, function(err,obj) {
    if (err)
        return res.send(err);

    if (obj == null)
    {
        console.log("null returned");
        return res.status(404).send("Oh uh, something went wrong");

    }
    console.log(obj);
    res.json(obj);
});

It is due to you sending multiple headers, as the error says, when err is present.

Akxe
  • 9,694
  • 3
  • 36
  • 71