How to do a duplicate email check in the Put (update) method when some other user already has the same email registered?

Asked

Viewed 82 times

-1

In the back-end of the application in Ode, I want to do an e-mail check when some other user already has the same email registered. Already works well when using with the POST method, however, when doing an update with PUT, the current routine identifies the user’s own email to be changed as duplicate (because it is already registered in the database), did not want this and does not make sense, I would like in the update, not be considered the email of the user I am changing, but yes, check if there is any other user with equivalent email.

I tried the following code:

chekEmailDuplicate = (req, res) => {
   User.findOne({//Tenta localizar algum usuário
            where: {//onde
                   id: !req.body.id,
                email: req.body.email
            }
        }).then(user => {
            if (user) {
                res.status(400).send("Fail -> Email is already in use!");
                return;
            }
}

As we can see in the code above, in:

id: !req.body.id 

I tried something like considering the different id of the user in modification, but it DOES NOT work, the application accepts email duplicity in the same way! No POST is a wonder, but with PUT will not.

Someone who knows Node or already knows how to solve could help me? Thanks in advance!

1 answer

1

I was able to solve :) I used sequelize.op, where I declared a variable:

const Op = db.sequelize.op

The section I used was like this: id:{[Op.ne]: req.body.id}

Below is a list of all possible sequelize operators.

Project.findAll({
  where: {
    id: {
      [Op.and]: {a: 5},           // AND (a = 5)
      [Op.or]: [{a: 5}, {a: 6}],  // (a = 5 OR a = 6)
      [Op.gt]: 6,                // id > 6
      [Op.gte]: 6,               // id >= 6
      [Op.lt]: 10,               // id < 10
      [Op.lte]: 10,              // id <= 10
      [Op.ne]: 20,               // id != 20
      [Op.between]: [6, 10],     // BETWEEN 6 AND 10
      [Op.notBetween]: [11, 15], // NOT BETWEEN 11 AND 15
      [Op.in]: [1, 2],           // IN [1, 2]
      [Op.notIn]: [1, 2],        // NOT IN [1, 2]
      [Op.like]: '%hat',         // LIKE '%hat'
      [Op.notLike]: '%hat',       // NOT LIKE '%hat'
      [Op.iLike]: '%hat',         // ILIKE '%hat' (case insensitive)  (PG only)
      [Op.notILike]: '%hat',      // NOT ILIKE '%hat'  (PG only)
      [Op.overlap]: [1, 2],       // && [1, 2] (PG array overlap operator)
      [Op.contains]: [1, 2],      // @> [1, 2] (PG array contains operator)
      [Op.contained]: [1, 2],     // <@ [1, 2] (PG array contained by operator)
      [Op.any]: [2,3]            // ANY ARRAY[2, 3]::INTEGER (PG only)
    },
    status: {
      [Op.not]: false           // status NOT FALSE
    }
  }
})

More information on: https://sequelize.org/master/manual/models-usage.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.