How to use like in where condition in sequelize, node js(如何在续集,节点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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在续集,节点js中的where条件下使用like
				
        
 
            
        - 在SQL中,如何为每个组选择前2行 2021-01-01
 - 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
 - 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
 - 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
 - 更改自动增量起始编号? 2021-01-01
 - 导入具有可变标题的 Excel 文件 2021-01-01
 - SQL 临时表问题 2022-01-01
 - 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
 - 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
 - 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
 
						
						
						
						
						
				
				
				
				