Reputation: 485
I want to search all columns and display the search result.
I am new to sequelize so used this code to do so, However here it is checking for full matches
I want to show details for partial matches also
How do i acheive this ?
router.post("/search-employees", async (req, res) => {
const searchTerm = req.body.searchTerm;
try {
const resp = await employee.findAll({
where: {
[Op.or]: [
{ name: searchTerm },
{ age: searchTerm },
{ country: searchTerm },
{ position: searchTerm },
{ wage: searchTerm },
],
},
});
res.status(200).send(resp);
} catch (e) {
res.status(400).send(e);
}
});
Upvotes: 1
Views: 563
Reputation: 450
You can use like operation e.g ([OP.LIKE], [OP.ILIKE]). Found Updated query below.
router.post("/search-employees", async (req, res) => {
const searchTerm = req.body.searchTerm;
try {
const resp = await employee.findAll({
where: {
[Op.or]: [
{ name: { [Op.like]: '%' + searchTerm + '%'} },
{ age: { [Op.like]: '%' + searchTerm + '%'} },
{ country: { [Op.like]: '%' + searchTerm + '%'} },
{ position: { [Op.like]: '%' + searchTerm + '%'} },
{ wage: { [Op.like]: '%' + searchTerm + '%'} }
],
},
});
res.status(200).send(resp);
} catch (e) {
res.status(400).send(e);
}
});
Upvotes: 2