Issue
I want to implement a getEntries endpoint with node.js, express, typescript, and Prisma client. when I implemented without pagination it works and returns a list of array, but when I implement with pagination, it returns successful but with an empty array of data. Below is what I have done
Dao
export const listApplicantsWithPagination = async (page: number, pageSize: number): Promise<Applicant[]> => {
  try {
    const parsedPage = typeof page === 'string' ? parseInt(page, 10) : page;
    const parsedPageSize = typeof pageSize === 'string' ? parseInt(pageSize, 10) : pageSize;
    console.log('Parsed Page:', parsedPage);
    console.log('Parsed PageSize:', parsedPageSize);
    const applicants = await db.applicant.findMany({
      select: {
        id: true,
        email: true,
        firstname: true,
        lastname: true,
        phone: true,
        country: true,
        role: true,
        github: true,
        linkedin: true,
        website: true,
        resume: true,
      },
      skip: (parsedPage + 10) * parsedPageSize, 
      take: parsedPageSize,
    });
    console.log('Prisma Query:', db.applicant.findMany());
    console.log('Retrieved Applicants:', applicants);
    return applicants;
  } catch (error) {
    console.error("Error listing applicants with pagination:", error);
    throw error.message;
  }
};
router
getEntriesRouter.get("/list-entries", authenticateToken, async (req: CustomRequest, res) => {
    try {
        const { page = 1, pageSize = 50 } = req.query;
        const parsedPage = parseInt(page as string, 10);
        const parsedPageSize = parseInt(pageSize as string, 10);
        if (isNaN(parsedPage) || isNaN(parsedPageSize) || parsedPage < 1 || parsedPageSize < 1) {
            return res.status(400).json({
                status: false,
                message: 'Invalid page or pageSize parameter.',
            });
        }
        const offset = (parsedPage + 1) * parsedPageSize;
        const applicants = await ApplicantDao.listApplicantsWithPagination(parsedPageSize, offset);
        return res.status(200).json({
            status: true,
            message: "Successfully retrieved applicants",
            data: applicants,
        });
    } catch (error: any) {
        return res.status(500).json({
            status: false,
            error: error.message,
        });
    }
});
                            Solution
There might be an issue with how you are calculating the skip parameter in your listApplicantsWithPagination function. The skip parameter should be calculated as (page - 1) * pageSize, but in your code, it seems to be calculated as (parsedPage + 1) * parsedPageSize.
const applicants = await db.applicant.findMany({
      select: {
        id: true,
        email: true,
        firstname: true,
        ...
      },
      skip: (parsedPage - 1) * parsedPageSize, // Corrected calculation here...
      take: parsedPageSize,
    })
Also in your router, you should pass parsedPage and parsedPageSize to the listApplicantsWithPagination function. I don't think you need a separate offset calculation because the listApplicantsWithPagination function internally handles pagination using the skip parameter in the Prisma query, as specified by (parsedPage - 1) * parsedPageSize.
const applicants = await ApplicantDao.listApplicantsWithPagination(parsedPage, parsedPageSize)
                            Answered By - Nazrul Chowdhury
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.