如何在注册控制器中添加条件以检查电子邮件是否已存在?

当用户尝试注册并输入详细信息时,将调用此API。我正在查看是否可以添加一个条件,说明电子邮件已经存在。

我想我需要类似的东西:

      const user = await User.find({ email })
      if (user) {
        res.status(400).json({ message: "User with this already exists" })
      }

但是我正在创建一个新用户,因此应将上述查询放在哪里。我对执行顺序感到困惑。

module.exports = {
    registerUser: async(req, res, next) => {
      try {
        var {
          username,
          email,
          password
        } = req.body
        if (password) {
          const salt = bcrypt.genSaltSync(10)
          password = bcrypt.hashSync(password, salt)
        }
        if (!username || !email || !password) {
          return res
            .status(400)
            .json({
              message: "Username, email and password are must"
            })
        }
        if (!validator.isEmail(email)) {
          return res.status(400).json({
            message: "Invaid email"
          })
        }
        if (password.length < 6) {
          return res
            .status(400)
            .json({
              message: "Password should be of at least 6 characters"
            })
        }
        const user = await User.create({
          username,
          email,
          password
        })
        if (!user) {
          return res.status(404).json({
            error: "No user found "
          })
        }
        return res.status(200).json({
          user
        })
      } catch (error) {
        return next(error)
      }
    }
评论
nquis
nquis

Place it inside the try block before the logic for registration because the find has the potentials of throwing and error if the email does not exist.

点赞
评论
有夫之夫
有夫之夫

I think the best solution will be to place it before any logic, also I changed res.json in if block to throw because throw stops execution of code below but res.json continues and that can cause potential problems. Also, I change Module.find() to Module.findOne() because find() returns empty array if nothing was found and if([emptyArray]) returns true

module.exports = {
    registerUser: async(req, res, next) => {
      try {
        var {
          username,
          email,
          password
        } = req.body
        const user = await User.findOne({
          email
        })
        if (user) {
          throw 'User with this already exists'
        }
        if (password) {
          const salt = bcrypt.genSaltSync(10)
          password = bcrypt.hashSync(password, salt)
        }
        if (!username || !email || !password) {
          return res
            .status(400)
            .json({
              message: "Username, email and password are must"
            })
        }
        if (!validator.isEmail(email)) {
          return res.status(400).json({
            message: "Invaid email"
          })
        }
        if (password.length < 6) {
          return res
            .status(400)
            .json({
              message: "Password should be of at least 6 characters"
            })
        }
        const user = await User.create({
          username,
          email,
          password
        })
        if (!user) {
          return res.status(404).json({
            error: "No user found "
          })
        }
        return res.status(200).json({
          user
        })
      } catch (error) {
        return next(error)
      }
    }
点赞
评论