title: A bug with Nodejs Pagination
body: <p>I'm trying to create this simple nodejs pagination with express and it works fine for all pages except the first page. When I get page 1 it keeps on loading without any result and idk whats the problem. Even youtube tutorials are writing the exact same code. Any help is appreciated</p>
  <pre><code>exports.paginatedResults = (model) =&gt; {
    return async (req, res, next) =&gt; {
      const page = parseInt(req.query.page)  || 1;
      const limit = req.query.limit * 1 || 100;
      const startIndex = (page - 1) * limit;
      const endIndex = page * limit;
      const results = {};
      if (endIndex &lt; await model.countDocuments().exec()) {
        results.next = {
          page: page + 1,
          limit: limit,
        }
      }
      if (startIndex &gt; 0) {
        results.previous = {
          page: page - 1,
          limit: limit,
        }
        results.results = await model.find().limit(limit).skip(startIndex).exec();
        res.paginatedResults = results;
  
        next();
      }
    }
  }
  </code></pre>