i want to filter, but that depend to the user, for example
Data.findAll({
where: {
name: {
[Op.or]: [
{ [Op.like]: ['%samsung%'] },
{ [Op.like]: ['%iphone%'] },
{ [Op.like]: ['%alcatel%']}
]
}
}
}
If the user selects only Samsung, how do I go about filtering only by Samsung?
CodePudding user response:
Assuming req.query.brands
stores either a single search string or an array of strings we can build Op.or
conditions on the fly:
const brands = [].concat(req.query.brands)
const brandConditions = brands.map(x => ({
[Op.like]: `%${x}%`
})
const foundItems = await Data.findAll({
where: {
name: {
[Op.or]: brandConditions
}
}
}