Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

158
Views
While testing API containing pagination in postman facing skip value error

I am using pagination middleware in my node js project but while testing api in postman it's throwing error skip value

Followed the paginate docs but still no use While testing api in which pagination is applied it's throwing error

below is my pagination middleware

    const pagination = (model) => {
  return async (req, res, next) => {
    const limit = parseInt(req.query.limit);
    const page = parseInt(req.query.page);

    if (page < 1 || page == undefined) {
      page = 1;
    }
    if (limit < 1 || limit == undefined) {
      limit = 2;
    }
    const startIndex = (page - 1) * limit;
    const endIndex = page * limit;
    const result = {};
    model.countDocuments({}, (err, count) => {
      const lengthOfDB = count;
      if (endIndex < lengthOfDB) {
        result.next = {
          page: page + 1,
          //limit: limit,
        };
      }
    }).clone();

    if (startIndex > 0) {
      result.previous = {
        page: page - 1,
        //limit: limit,
      };
    }
    try {
      result.result = await model.find().limit(limit).skip(startIndex).exec();
      res.json(result);
    } catch (e) {
      res.status(500).json({ message: e.message });
    }
  };
};
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If page is not provided in the query, it will end up being NaN, since this code does not cover that case:

// 'req.query.page' is undefined
let page = parseInt(req.query.page);
if (page < 1 || page == undefined) {
    page = 1;
}
// 'page' is now equal to NaN

A correct way to handle this could be:

let page = parseInt(req.query.page);
if (page < 1 || isNaN(page)) {
    page = 1;
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!