Here is the code It was working fine before I used same method in my other project but don't know what went wrong Note: if I don't apply filter then result is not empty.
async findAllCategories(req, res) {
try {
const { page = 1, perPage = 10, filter, sortField, sortDir } = req.query;
const options = { page: parseInt(page, 10), limit: parseInt(perPage, 10) };
const query = {};
if (filter) {
query.item = {
$regex: filter
}
}
if (sortField && sortDir) {
options.sort = {
[sortField]: sortDir
}
}
console.log(query, options);
const categories = await Category.paginate(query, options);
return res.json(categories);
} catch (err) {
return res.status(INTERNAL_SERVER_ERROR).json(err);
}
}