1

I trying get all users by provided emails using where clause and $or in sequelize.

Where in sequelize

So i created query from my emails array for multiple where like this:

[ { email: 'email1@gmail.com' }, { email: 'email@gmail.com' } ]

Then i try to get it in many way for example like that:

const allExistedUsers = await User.findAll({
  where: { $or: multileWhere }
});

According to answer in this post and doc it should work:

{ $and: [{"Key1": "Value1"}, {"Key2": "Value2"}] }

But it does not work. I can't figure out how to do it.

Turtl3
  • 11
  • 3

2 Answers2

1

Your E-Mails as Array:

const mails = [ { email: 'email1@gmail.com' }, { email: 'email@gmail.com' } ];
const yourEmailsArray = mails.map( el => { return el.email; });

Then you can concat your E-Mails. Sequelize formats in:

WHERE (`User`.`email` = 'email1@gmail.com' OR `User`.`email` = 'email2@gmail.com')
const allExistedUsers = await User.findAll({
 where: {      
           email: { [Op.or]: [].concat(yourEmailsArray)}
       }
});
helvete
  • 2,455
  • 13
  • 33
  • 37
B.Irneos
  • 31
  • 6
0

I did it using another approach from this post here

const allExistedUsers = await User.findAll({
  where: { email: { [Op.or]: usersEmailsToAssign } }
});

usersEmailsToAssign is a array with strings

Turtl3
  • 11
  • 3