next.js 跳过和采取不与include一起工作的选项

b4wnujal  于 2023-06-22  发布在  其他
关注(0)|答案(1)|浏览(104)
const inventory = await prisma.inventory.findMany({
  skip: (page - 1) * limit,
  take: JSON.parse(limit),
  include: {
    warehouses_stock: true,
  },
});

return NextResponse.json(inventory);

分页工作正常,但warehouses_stock返回一个空数组。
我删除了skip and take选项,它似乎正在返回仓库_stock。

6ovsh4lw

6ovsh4lw1#

将逻辑存储在变量中:

const skip = (page - 1) * limit;

然后这样使用:

const page = parseInt(req.nextUrl.searchParams.get("page"));
  const limit = parseInt(req.nextUrl.searchParams.get("limit"));
  const skip = (page - 1) * limit;

    const inventory = await prisma.inventory.findMany({
      skip: skip,
      take: limit,
      include: {
        warehouses_stock: true,
      },
    });

    return NextResponse.json(inventory);

相关问题