i have a controller that i want to search or find with condition. Can anybody help me or give me some suggestions so that i can find data with more conditions. Let me demonstrate my code below:
// Find all car with condition
export function findAllCar( req, res){
const name = req.query.name;
const color = req.query.color;
const brand = req.query.brand;
var condition = name ? { name: { [Op.iLike]: `%${name}%` } } : null;// Here i only can find with name, now i want to add more condition are color and brand
Car.findAll({ where: condition })
.then(data => {
res.send(data);
})
.catch(err => {
res.status(500).send({
message:
err.message || "Some error occurred while retrieving CARS."
});
});
}
How can i set the condition more with COLOR and BRAND. I am very appreciate for every help
CodePudding user response:
Try adding a comma:
var condition = name && color && brand ? {
name: { [Op.iLike]: `%${name}%` },
color: { [Op.iLike]: `%${color}%` },
brand: { [Op.iLike]: `%${brand}%` },
} : null;