Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use like in where condition in sequelize, node js

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.

like image 831
Harinarayan Avatar asked Oct 27 '25 03:10

Harinarayan


1 Answers

you can use Op operator in query
like :-

const Op = Sequelize.Op

{
  [Op.or]: [
    {
      fieldName: {
        [Op.like]: 'abc%'
      }
    },
    {
      fieldName: {
        [Op.like]: '%abc%'
      }
    }
  ]
}
like image 54
vimmi Avatar answered Oct 29 '25 19:10

vimmi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!