How to add or condition using sequelize in Reactjs

Viewed 108

I would like to add the below query condition in my “service/players” get userModel.findAll()...

select * from user where privilege =‘ADMIN’ or privilege = ‘PLAYER’

The below query just returns only PLAYER.

app.get('/service/players', async (req, res) => {
  try {
    const players = await UserModel.findAll({ where: { privilege: 'PLAYER' } });
    res.status(200).json({ players });
  } catch (e) {
    res.status(500).json({ message: e.message });
  }

});
1 Answers

You can use Op.or or Op.in

Like this

app.get('/service/players', async (req, res) => {
    try {
      const players = await UserModel.findAll({
        where: {
          privilege: {
            [Op.or]: ['ADMIN', 'PLAYER']
          }
        }
      })
      res.status(200).json({ players });
    } catch (e) {
      res.status(500).json({ message: e.message });
    }
}

or simply like this

where: {
    privilege: ['ADMIN', 'PLAYER']
  }
Related