title: Nodejs Pagination and Search
body: <p>Hi I am implementing a Pagination and Search on the server side with the following code below. It works for me however with a small bug.</p>
  <p>For example: <br>
  page 1: user1, user2, user3 <br>
  page 2: user4, user5, user6</p>
  <p>The whole db contains 6 users, but when i try to do a filter by user5, if i am on page 1 it wont be able to search for user5, only when i go to page2, will user5 appear. i would like to do a filter first and slice base on the filter but am facing difficulties.</p>
  <p>do appreciate help thanks</p>
  <pre><code>router.route('/').get(async (req, res) =&gt; {
    try {
      let query = User.find();
  
      const filter = req.query;
      const page = parseInt(req.query.page) || 1;
      const pageSize = parseInt(req.query.limit) || 10;
      const skip = (page - 1) * pageSize;
      const total = await User.countDocuments();
      const pages = Math.ceil(total / pageSize);
  
      if (page &gt; pages) {
        return res.status(404).json({
          status: &quot;fail&quot;,
          message: &quot;No page found&quot;,
        });
      }
  
      result = await query.skip(skip).limit(pageSize);
      const filtered = result.filter(target =&gt; {
        if (filter.filter === &quot;&quot;) {
          return target;
        }
        else if (target.name.toLowerCase().includes(filter.filter)){
          return target;
        }
        else if (target.email.toLowerCase().includes(filter.filter)){
          return target;
        }
      })
      
      res.json({
        status: &quot;success&quot;,
        filter,
        count: result.length,
        page,
        pages,
        data: filtered
      });
    } catch (error) {
      console.log(error);
      res.status(400).json({
        status: &quot;error&quot;,
        message: &quot;Server Error&quot;,
      });
    }
  });
  
  </code></pre>
  <p><a href="https://i.stack.imgur.com/ZNc0B.png" rel="nofollow noreferrer">it should return one user here</a></p>
  <p><a href="https://i.stack.imgur.com/luVP3.png" rel="nofollow noreferrer">it only returns that user in page 2</a></p>
  <p>I am expecting that the user be return no matter where it is in the pages.</p>