0

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);
    }
  }
2
  • Did you add paginate to the schema? Commented Oct 15, 2020 at 17:16
  • Yes have a look. CategorySchema.plugin(mongoosePaginate); Commented Oct 15, 2020 at 18:59

1 Answer 1

1

Solved it. It was a simple mistake in query.item replaced it with query.name. It should be according to the field declared in model. I am still in learning phase.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.