Home > Net >  Mongoose findOne always return undefined
Mongoose findOne always return undefined

Time:11-21

When ever i tried to post this API via postman using email and password.

I get User Not Found. I cross checked email and password 100 times.

In command prompt i am getting UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client.

What i am doing wrong here. Please tell me, Thank you in advance.

exports.loginUser = async (req, res) => {
    try{
      const user = await userTable.findOne({ email:req.body.email });
      if (!user){
        res.send({
          status: 404,
          message: "User Not Found"
        });
      }

      const hashpass = cryptr.decrypt(user.password);
      if (hashpass == req.body.password){

        const accessToken = jwt.sign({ id: user._id, email: user.email }, process.env.JWT_PASS, {expiresIn:"1d"});
        res.send({
          status: 200,
          data: user,
          jwt: accessToken
        });
      }else{
        res.send({
          status: 404,
          message: "Wrong password"
        });
      }
    }catch(err){
      res.status(500).send({
        status: 0,
        message: "catch issue"   err.message
      });
    };
};

CodePudding user response:

I'm pretty sure this is happening because your handler continues after sending a response, therefore attempting to modify a response after it's already been sent.

Keep in mind, it could be how you are using the loginUser function. If you wouldn't mind updating your question showing us how you are using it, that would be super helpful!

There are a couple things that come to mind; 1. You may need to add a return statement somewhere, like on line 10/11 for example 2. You could also wrap the code after the first if statement inside an else statement to see if that changes things.

At the end of the day, I'm pretty sure your code is continuing after sending a response, which is why you see that error.

With return

exports.loginUser = async (req, res) => {
  try {
    let dataToSend = {};
    const user = await userTable.findOne({ email: req.body.email });
    if (!user) {
      res.send({
        status: 404,
        message: "User Not Found",
      });
      // Try adding this
      return;
    }
    const hashpass = cryptr.decrypt(user.password);
    if (hashpass == req.body.password) {
      const accessToken = jwt.sign({ id: user._id, email: user.email }, process.env.JWT_PASS, { expiresIn: "1d" });
      res.send({
        status: 200,
        data: user,
        jwt: accessToken,
      });
    } else {
      res.send({
        status: 404,
        message: "Wrong password",
      });
    }
  } catch (err) {
    res.status(500).send({
      status: 0,
      message: "catch issue"   err.message,
    });
  }
};

Wrap in else

This is very ugly and should only be used to test in my opinion!

exports.loginUser = async (req, res) => {
  try {
    const user = await userTable.findOne({ email: req.body.email });
    if (!user) {
      res.send({
        status: 404,
        message: "User Not Found",
      });
    } else {
      const hashpass = cryptr.decrypt(user.password);
      if (hashpass == req.body.password) {
        const accessToken = jwt.sign({ id: user._id, email: user.email }, process.env.JWT_PASS, { expiresIn: "1d" });
        res.send({
          status: 200,
          data: user,
          jwt: accessToken,
        });
      } else {
        res.send({
          status: 404,
          message: "Wrong password",
        });
      }
    }
  } catch (err) {
    res.status(500).send({
      status: 0,
      message: "catch issue"   err.message,
    });
  }
};
  • Related