Home > database >  nodemon app crashed after logging in with false credentials
nodemon app crashed after logging in with false credentials

Time:12-11

if i put bad credentials that are different then information in the databse , i got this error :

` node:internal/process/promises:246 triggerUncaughtException(err, true /* fromPromise */); ^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at new NodeError (node:internal/errors:363:5) at ServerResponse.setHeader (node:_http_outgoing:574:11) at ServerResponse.header (C:\Users\Chadi Seif\node_modules\express\lib\response.js:771:10) at ServerResponse.send (C:\Users\Chadi Seif\node_modules\express\lib\response.js:170:12) at ServerResponse.json (C:\Users\Chadi Seif\node_modules\express\lib\response.js:267:15) at ServerResponse.send (C:\Users\Chadi Seif\node_modules\express\lib\response.js:158:21) at exports.loginAdmin (C:\Users\Chadi Seif\Desktop\testdelete\test2\controllers\admin.js:58:21) at processTicksAndRejections (node:internal/process/task_queues:96:5) { code: 'ERR_HTTP_HEADERS_SENT' } [nodemon] app crashed - waiting for file changes before starting... `

here is my code


exports.loginAdmin = async (req, res) => {
  try {
    const { email, password } = req.body;
    if (!(email && password)) {
      res.status(400).send({ error: [{ msg: " inputs are required" }] });
    }
    const userfound = await Admin.findOne({ email: req.body.email });
    if (!userfound) {
      res.status(400).send({ error: [{ msg: "bad credentials" }] });
    }
    //Compare passwords :
    const compare = await bcrypt.compare(password, userfound.password);
    if (compare) {
      token = jwt.sign({ id: userfound._id }, process.env.TOKEN_KEY, {
        expiresIn: "1days",
      });
      res.status(201).send({ msg: "Happy to see you", userfound, token });
    } else {
      res.status(503).send({ error: [{ msg: "bad credentials" }] });
    }
  } catch (error) {
    res.status(400).send({ error: [{ msg: error }] });
  }
};```


please  i need your help community !

CodePudding user response:

The error means that thing before that code have already returned some headers/content back to the browser and when you try to set a new status code you get this error.

So, do examine your request pipeline or look in the browser what has already been sent?

also

You should not return a 503 status here

res.status(**503**).send({ error: [{ msg: "bad credentials" }] });", 

typo?

CodePudding user response:

This error throw because you donnot have a return in your function. just put res.status(...).send(...) doest not end your function execution. by this way, your function try to set header for sending response every time where we have res.status(...).send(...)

So, to fix that, you should return response at any time you want to send back response to client like this:

 return res.status(400).send({ error: [{ msg: " inputs are required" }] });

Your code should be like

exports.loginAdmin = async (req, res) => {
  try {
    const { email, password } = req.body;
    if (!(email && password)) {
      return res.status(400).send({ error: [{ msg: " inputs are required" }] });
    }
    const userfound = await Admin.findOne({ email: req.body.email });
    if (!userfound) {
      return res.status(400).send({ error: [{ msg: "bad credentials" }] });
    }
    //Compare passwords :
    const compare = await bcrypt.compare(password, userfound.password);
    if (compare) {
      token = jwt.sign({ id: userfound._id }, process.env.TOKEN_KEY, {
        expiresIn: "1days",
      });
      return res.status(201).send({ msg: "Happy to see you", userfound, token });
    } else {
     return res.status(503).send({ error: [{ msg: "bad credentials" }] });
    }
  } catch (error) {
    return res.status(400).send({ error: [{ msg: error }] });
  }
};`

CodePudding user response:

Thank you for your reply , i found the answer ... the problem was related to the response sent here :

if (!userfound) { res.status(400).send({ error: [{ msg: "bad credentials" }] }); }

because when catching error the response is sent for the second time , and that crashes the Server

  • Related