Home > Blockchain >  I can't handle this error internal/errors:464
I can't handle this error internal/errors:464

Time:09-28

When I try authentication in express, the following errors appear. but if successfully log in there is no error.

here is my code:

exports.loginUser = 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);
}

};

And here is error: Server crush

CodePudding user response:

after res.status(400).json() you should type return so the rest of the code won't be executed.

exports.loginUser = async (req, res) => {
try {
const user = await User.findOne({ username: req.body.username });
 if (!user) {
   res.status(400).json("Wrong credentials!")
  return;
 }

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

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


} catch (err) {
   res.status(500).json(err);
}
  • Related