Reputation: 121
I am trying to filter the data from table in which i want to use multiple condition. when i apply like in query it show application error. how to do the same?
I am using nodejs framework, express, sequelize and mysql.
router.get('/booking-information', function (req, res) {
// Get orders
Promise.all([Order.findAll({
/*where: {
endDate: null,
},*/
order: [
['id', 'DESC'],
]
}),
Professional.findAll({where: {status : '1'}})
])
.then(([orders, professionals]) => {
orders.map((order) => {
let professionalsInSameArea = professionals.filter((professional) => {
return (professional.service === order.service || professional.secondary_service LIKE '%' + order.service + '%') && (professional.area === order.area || order.area === professional.secondary_area);
});
order.professionals = [...professionalsInSameArea]
return order;
});
res.render('booking-information', {title: 'Technician', orders: orders, user: req.user});
})
.catch((err) => {
console.error(err);
});
});
I want to filter out the professionals in same area and same service for which a order placed.
Upvotes: 4
Views: 7499
Reputation: 12542
Believe it or not, You can just use String.indexOf
function in your case Because
By definition, String LIKE %word%
means if the String
contains word
:
router.get('/booking-information', function (req, res) {
// Get orders
Promise.all([
Order.findAll({
/*where: {
endDate: null,
},*/
order: [
['id', 'DESC'],
]
}),
Professional.findAll({
where: {
status: '1'
}
})
])
.then(([orders, professionals]) => {
orders.map((order) => {
let professionalsInSameArea = professionals.filter((professional) => {
return (professional.service === order.service
|| (professional.secondary_service || '').toLowerCase().indexOf((order.service || '').toLowerCase()) > -1)
&& (professional.area === order.area
|| order.area === professional.secondary_area);
});
order.professionals = professionalsInSameArea; //you don't need to spread, then make an array
return order;
});
res.render('booking-information', { title: 'Technician', orders: orders, user: req.user });
})
.catch((err) => {
console.error(err);
});
});
Upvotes: 3
Reputation: 407
you can use Op operator in query
like :-
const Op = Sequelize.Op
{
[Op.or]: [
{
fieldName: {
[Op.like]: 'abc%'
}
},
{
fieldName: {
[Op.like]: '%abc%'
}
}
]
}
Upvotes: 4