From 90dbadac24200c1608b9b35ea56f2ee22a1f1b0e Mon Sep 17 00:00:00 2001 From: Spacelord Date: Sun, 21 May 2023 01:31:35 +0200 Subject: [PATCH] Fixed the redirection loop in the pagination of /items A redirection loop occurred when there were no items in the database. --- src/routes/frontend/items.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/routes/frontend/items.ts b/src/routes/frontend/items.ts index 2f62c30..5305f17 100644 --- a/src/routes/frontend/items.ts +++ b/src/routes/frontend/items.ts @@ -14,15 +14,14 @@ async function get(req: Request, res: Response) { const takeSize = 25; // Amount of times per page const pageSize = Math.ceil(itemCount / takeSize); // Amount of pages, always round up - // If page is less then 1 or more then the max page size redirect to first or last page + // If page is less then 1 or more then the max page size redirect to first or last page. If itemCount is 0 do not redirect. if (page < 1) { res.redirect('?page=1'); return; - } else if (page > pageSize) { + } else if (page > pageSize && itemCount !== 0) { res.redirect('?page=' + pageSize); return; } - prisma.item .findMany({ skip: (page - 1) * takeSize, take: takeSize }) // Skip the amount of items per page times the page number minus 1; skip has to be (page-1)*takeSize because skip is 0 indexed .then((items) => {