I am currently working on a solution that involves the following code:
export const ProductsByFilter = async (req, res) => {
const {a, b, c} = req.query
let query = {}
if (a) {
query.a = a;
}
if (b) {
query.b = b;
}
if (c) {
query.c = c;
}
const products = await Product.find(query);
res.status(200).json(products);
}
The challenge I'm facing is that a
is a String and b
is a Number, so they work fine when making multiple requests even if one is empty. However, c
is an array and it's not functioning properly. How can I adjust the logic to make c
work as well, while still allowing for multiple query requests that ignore empty or null parameters?