Fixed the redirection loop in the pagination of /items
A redirection loop occurred when there were no items in the database.
This commit is contained in:
parent
4145dafb7d
commit
90dbadac24
@ -14,15 +14,14 @@ async function get(req: Request, res: Response) {
|
|||||||
const takeSize = 25; // Amount of times per page
|
const takeSize = 25; // Amount of times per page
|
||||||
const pageSize = Math.ceil(itemCount / takeSize); // Amount of pages, always round up
|
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) {
|
if (page < 1) {
|
||||||
res.redirect('?page=1');
|
res.redirect('?page=1');
|
||||||
return;
|
return;
|
||||||
} else if (page > pageSize) {
|
} else if (page > pageSize && itemCount !== 0) {
|
||||||
res.redirect('?page=' + pageSize);
|
res.redirect('?page=' + pageSize);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
prisma.item
|
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
|
.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) => {
|
.then((items) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user