Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

router unique validation for mongoose

router.post("/register", async (req, res) => {
  try {
    const { email, password } = req.body;

    let user = await User.findOne({ email });
    if (user) return res.status(400).send("User already registered.");

    user = new User({ email, password });
    user.password = await bcrypt.hash(user.password, 10);
    await user.save();

    res.send("registered");
  } catch (err) {
    console.log(err);
    res.status(500).send("Something went wrong");
  }
});
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #router #unique #validation #mongoose
ADD COMMENT
Topic
Name
9+8 =