Query not printed as expected
exports.getAllProducts = async (req, res) => {
try {
console.log(req.query);
const queryObj = { ...req.query };
const excludedFilters = ["page", "limit", "fields", "sort"];
excludedFilters.forEach((el) => delete queryObj[el]);
let queryStr = JSON.stringify(queryObj);
queryStr = queryStr.replace(
/\b(gt|gte|lt|lte|ne|eq)\b/g,
(match) => `$${match}`
);
const products = await Product.find(JSON.parse(queryStr));
res.status(200).json({ message: 'success', data: products });
} catch (error) {
res.status(500).json({ message: 'error', error });
}};
When I tried to use a query to make sure everything is working as expected, the query was ?price[eq]=5 and returned nothing. When printing it to debug, it was printed like this { 'price[eq]': '5' }
Express version is 5.1.0
is there any method to format it as expected so that it will be price: { gte: 5 }
after doing some research, I believe that it should have been converted by default