where搜索的续集语法

ibrsph3r  于 2021-10-10  发布在  Java
关注(0)|答案(0)|浏览(172)

大家好!当然,在浏览过网络之后,有一个愚蠢的小问题。
对于sequelize,我想在where中添加第二个参数。也就是说,目前我只是在寻找电子邮件。我想添加用户名到它。以下是我的基本代码片段:

exports.signup = (req, res) => {
models
    .findOne({
      attributes: ['email'],
      where: {
        email:  Buffer.from(req.body.email).toString("hex")
      }
    })
 }

我想要这样的东西:

models
    .findOne({
      attributes: ['email'] ['username'],
      where: {
      username: username,
        email:  Buffer.from(req.body.email).toString("hex")
      }
    })
.findOne({
  attributes: ['email', 'username' ], // --> instead of ['email'] ['username']
  where: {
    username: req.body.username, // -> If you are getting username from the request body
    email:  Buffer.from(req.body.email).toString("hex")
  }
})

    .then(

      ((userFound) => {

          if (!userFound ) {

            bcrypt.hash(password, 10, function (err, bcryptPassword) {

              const newUser = models
                .create({
                  username: username,
                  email: Buffer.from(req.body.email).toString("hex"),
                  password: bcryptPassword,
                  bio: bio,
                  admin: false
                })

                .then((newUser) => {
                  res.status(201).json({
                    userId: newUser.id,
                    message: 'Merci, votre inscription est bien pris en compte !'
                  })
                })
                .catch((err) => {
                  res.status(500).json({
                    error: "Impossible d'ajouter un utilisateur",
                  })
                })

            })

          } else {
            return res.status(409).json({
              error: 'Ce compte existe déjà ',
            })
          }

      })
    )

}

我尝试了几种语法,也尝试了findall方法,但我一定会出错。
提前谢谢。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题