79138875

Date: 2024-10-29 20:23:14
Score: 1
Natty:
Report link

Why don't you want to check for existing username directly in the MongoDB query like below.

try{
  const existingUser = await mongodb
  .getDb()
  .db()
  .collection("users")
  .findOne({ userName: req.body.userName });

  if (existingUser) {
    return res.status(400).json({ error: "Username is already in use" });
  }
  // rest of the code
} catch (error) {
  res.status(500).json(error);
  console.log(error);
}
Reasons:
  • Has code block (-0.5):
  • Starts with a question (0.5): Why don't you
  • Low reputation (1):
Posted by: Coder365