Home > database >  Error [ERR_HTTP_HEADERS_SENT] : Can't figure out the multipe requests
Error [ERR_HTTP_HEADERS_SENT] : Can't figure out the multipe requests

Time:08-28

I have this error : Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client. From my understanding, the problem is that I am trying to send more than one response to the same http request. My instinct tell me that it’s this part that messes up :

catch (err) {
   res.status(400).json(err);
  }

Because if no user/password found in the DB, we already send status(400). Am I right ? More importantly (and that’s what drives me crazy), I am following a YT tuto and his code is exactly like mine, yet his seems to be working without any problem.

My code :

    const router = require("express").Router();
const User = require("../models/Users");
const bcrypt = require("bcrypt");


//LOGIN
router.post("/login", async (req, res) => {
  try {
    const user = await User.findOne({ username: req.body.username });
    !user && res.status(400).json("Wrong credentials!");

    const validated = await bcrypt.compare(req.body.password, user.password);
    !validated && res.status(400).json("Wrong credentiaaaals!");

    const { password, ...others } = user._doc;
     res.status(200).json(others);
  } catch (err) {
   res.status(500).json(err);
  }
});

module.exports = router;

His code :

//LOGIN
router.post("/login", async (req, res) => {
  try {
    const user = await User.findOne({ username: req.body.username });
    !user && res.status(400).json("Wrong credentials!");

    const validated = await bcrypt.compare(req.body.password, user.password);
    !validated && res.status(400).json("Wrong credentials!");

    const { password, ...others } = user._doc;
    res.status(200).json(others);
  } catch (err) {
    res.status(500).json(err);
  }
});

module.exports = router;

Am I doing something wrong ? Is my reflexion bad ? Thanks !

CodePudding user response:

You are right, your code is trying to send data to the client multiple times. The issue is that after the call .json("Wrong credentials!") completed, the write stream to the client will be closed, and you will not be able to send any other data to the client. The framework knows to detect it and show you the bug.

In your code, after the method .json("Wrong credentials!") finishes own execution, your program will continue and will try to execute the next lines...

You just need to add return, so the program will exit the current flow after it sends the response to the client.

const router = require("express").Router();
const User = require("../models/Users");
const bcrypt = require("bcrypt");

//LOGIN
router.post("/login", async (req, res) => {
  try {
    const user = await User.findOne({ username: req.body.username });
    if (!user) {
      return res.status(400).json("Wrong credentials!"); // without return the code will continue to execute next lines
    }

    const validated = await bcrypt.compare(req.body.password, user.password);
    if (!validated) {
      return res.status(400).json("Wrong credentiaaaals!"); // without return the code will continue to execute next lines
    }

    const { password, ...others } = user._doc;
    res.status(200).json(others); // return is not necessary, because there is no cod which will be executed after we back from the json method
  } catch (err) {
    res.status(500).json(err); // return is not necessary, because there is no cod which will be executed after we back from the json method
  }
});

module.exports = router;
  • Related