title: howto paginate my search result in handlebars
body: <p>When i search---products are displaying..but when i click on page 2 it gets error...how can i paginate the search results in handlebars..</p>
  
  <p>This is my search route.</p>
  
  <pre><code>router.get('/products-search',function(req,res,next){
      const perPage = 1;
      const page = req.query.page || 1;
      const title = "Ahl.com: "+ req.query['search'];
      Product.find({"tags" : {"$regex": req.query['search'], "$options": "i"}}).sort('-date').skip((perPage * page) - perPage).limit(perPage)
      .then(products =&gt; {
          Product.countDocuments({"tags" : {"$regex": req.query['search'], "$options": "i"}}).then(productCount=&gt;{
              res.render("categories/search", {title:title , products:products , current: parseInt(page), pages:Math.ceil(productCount / perPage)});
          });
      })
      .catch(err =&gt; console.log("Search err"));
  </code></pre>
  
  <p>})</p>
  
  <p>my handlebar helper for pagination</p>
  
  <pre><code>paginate: function(options){
          let output = "";
  
          if(options.hash.current === 1){
              output += `&lt;li class="page-item disabled"&gt;&lt;a class="page-link"&gt;F&lt;/a&gt;&lt;/li&gt;`;
          }else{
              output += `&lt;li class="page-item"&gt;&lt;a href="?page=1" class="page-link"&gt;F&lt;/a&gt;&lt;/li&gt;`;
          }
  
          let i = (Number(options.hash.current) &gt; 5 ? Number(options.hash.current) - 4 : 1);
  
          if(i !== 1){
              output += `&lt;li class="page-item disabled"&gt;&lt;a class="page-link"&gt;....&lt;/a&gt;&lt;/li&gt;`;
          }
  
          for(; i &lt;= (Number(options.hash.current) + 4) &amp;&amp; i &lt;= options.hash.pages; i++){
              if(i === options.hash.current){
                  output += `&lt;li class="page-item active"&gt;&lt;a class="page-link"&gt;${i}&lt;/a&gt;&lt;/li&gt;`;
              }else{
                  output += `&lt;li class="page-item"&gt;&lt;a href="?page=${i}" class="page-link"&gt;${i}&lt;/a&gt;&lt;/li&gt;`;
              }
              if(i === Number(options.hash.current) + 4 &amp;&amp; i &lt; options.hash.pages){
                  output += `&lt;li class="page-item disabled"&gt;&lt;a class="page-link"&gt;....&lt;/a&gt;&lt;/li&gt;`;
              }
          }
  
          if(options.hash.current === options.hash.pages){
              output += `&lt;li class="page-item disabled"&gt;&lt;a class="page-link"&gt;L&lt;/a&gt;&lt;/li&gt;`;
          }else{
              output += `&lt;li class="page-item"&gt;&lt;a href="?page=${options.hash.pages}" class="page-link"&gt;L&lt;/a&gt;&lt;/li&gt;`;
          }
  
          return output;
      }
  </code></pre>
  
  <p>pagination links</p>
  
  <pre><code>&lt;div class="container"&gt;
    &lt;nav aria-label="Page navigation example"&gt;
      &lt;ul class="flex-wrap pagination"&gt;
          {{#paginate current=current pages=pages}}{{/paginate}}
  
      &lt;/ul&gt;
    &lt;/nav&gt;
  &lt;/div&gt;
  </code></pre>