本文介绍了如何在续集,节点js中的where条件下使用like的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从要使用多个条件的表中过滤数据.当我在查询中申请时,它显示应用程序错误.怎么做?
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?
我正在使用 nodejs 框架、express、sequelize 和 mysql.
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.
推荐答案
信不信由你,你可以使用 String.indexOf
函数在你的情况下因为
Believe it or not, You can just use String.indexOf
function in your case Because
根据定义,String LIKE %word%
表示如果 String
包含 word
:
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);
});
});
这篇关于如何在续集,节点js中的where条件下使用like的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!