From 2d8cf41a83dc71df1c838f411e774226b68b8e27 Mon Sep 17 00:00:00 2001 From: Sonny Date: Sat, 16 Dec 2023 22:53:32 +0100 Subject: [PATCH] fix: error when user has no category --- src/pages/api/category/index.ts | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/pages/api/category/index.ts b/src/pages/api/category/index.ts index a2b7436..177ba9d 100644 --- a/src/pages/api/category/index.ts +++ b/src/pages/api/category/index.ts @@ -25,7 +25,7 @@ async function createCategory({ req, res, user }) { throw new Error('Category name already used'); } - const { id: lastCategoryId } = await prisma.category.findFirst({ + const lastCategory = await prisma.category.findFirst({ where: { authorId: user.id, nextId: null, @@ -39,14 +39,16 @@ async function createCategory({ req, res, user }) { data: { name, authorId: user.id }, }); - await prisma.category.update({ - where: { - id: lastCategoryId, - }, - data: { - nextId: categoryCreated.id, - }, - }); + if (lastCategory) { + await prisma.category.update({ + where: { + id: lastCategory.id, + }, + data: { + nextId: categoryCreated.id, + }, + }); + } return res.status(200).send({ success: 'Category successfully created',