I have created a html page to filter some data in MongoDb and render the data to a display page. And set pagination in the display page. Whenever I choose a new page number, the URL append the page number in the pagination.
Index.js
let results = await db.collection("bookings").find(whereClause,
{
limit: perPage,
skip: perPage * (Math.max(req.query.page - 1, 0) || 0)
}
).toArray();
var fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;
return res.render('search', { bookings: results, pages: pages, perPage: perPage, fullUrl:fullUrl });
Pagination Page:
<nav aria-label="Page navigation example">
<ul class="pagination flex-wrap">
<% for (var i = 1; i <= pages; i++) { %>
<li class="page-item"></li>
<a class="page-link" href="<%=fullUrl%>&perPage=<%=perPage%>&page=<%= i %>">
<%= i %>
</a>
</li>
<% } %>
</ul>
</nav>
The problem is every time when I click a new page number in the pagination, it will append the perPage and page parameter into the original link.
http://localhost:3000/search?numTickets=1&name=somename&perPage=6&page=5&perPage=6&page=3
2
Answers
Try to remove the
perPage
andpage
parameters from thefullUrl
before returning it:Parse the original URL into an URL instance. Then you can more easily manipulate the
searchParams
and in your template…