Node.js 分页返回空数组

问题描述 投票:0回答:1

我想使用 node.js、express、typescript 和 Prisma 客户端实现 getEntries 端点。当我在没有分页的情况下实现时,它可以工作并返回一个数组列表,但是当我在分页的情况下实现时,它会成功返回,但带有空数据数组。以下是我所做的

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;
  }
};

路由器

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,
        });
    }
});
node.js typescript express pagination prisma
1个回答
0
投票

您在 listApplicantsWithPagination 函数中计算 Skip 参数的方式可能存在问题。跳过参数应计算为 (page - 1) * pageSize,但在您的代码中,它似乎计算为 (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,
    })

同样在路由器中,您应该将 parsedPage 和 parsedPageSize 传递给 listApplicantsWithPagination 函数。我认为您不需要单独的偏移量计算,因为 listApplicantsWithPagination 函数在内部使用 Prisma 查询中的 Skip 参数处理分页,如 (parsedPage - 1) * parsedPageSize 指定。

const applicants = await ApplicantDao.listApplicantsWithPagination(parsedPage, parsedPageSize)
© www.soinside.com 2019 - 2024. All rights reserved.