0

I don't know why it's not working while trying to do a match inside array of strings which comes from client's request query. This is what I've tried to do filter from my database

const serviceTypes = ["Hybrid", "Special", "Normal"];

if (req.query.serviceTypes) {
   serviceTypes = req.query.serviceTypes;
}
....

const packagesList = await PackagesList.aggregate([
....
// here I did an aggregation which works all fine if there is no req.query.serviceTypes
....

      {
        $match: {
          isActive: true,
          "packageService.serviceType": {
            $in: serviceTypes,  // I've also tried to directly feed `JSON.parse(req.query.serviceTypes)`
          },
        },
      },

What confuses me is it works well if I didn't send any request query in the header and I tried to see what it gets in my console and the output is all fine. i.e if I send this in the request serviceTypes=['Hybrid', 'Normal']

my console.log(JSON.parse(req.query.serviceTypes)); output looks like

['Hybrid', 'Normal']

But the result is an empty array even though there exist a valid data in my database.

Kirubel
  • 1,411
  • 8
  • 31

0 Answers0