PrismaP
Prisma11mo ago
5 replies
Logan

Prisma not getting past create function.

Hello, I'm using NextJS API routes and I'm having an issue creating record in the database. I am seeing the console.log("Test2") in the console but getting a 400 response code. I'm not sure what is causing this.

export async function POST(request) {
  const { uuid, product } = await request.json();

  console.log(uuid + " " + product);
  try {
    const findDevice = prisma.device.count({
      where: {
        uuid: uuid,
      },
    });

    if (findDevice > 0) {
      console.log("Test");
      const updateDevice = await prisma.device.update({
        where: {
          uuid: uuid,
        },
        data: {
          product: product,
          lastUpdated: Date.now(),
        },
      });
      console.log("Updated device");
    } else {
      console.log("Test 2");
      const createDevice = await prisma.device.create({
        data: {
          uuid: uuid,
          product: product,
        },
      });
      console.log("Create new device");
    }

    const response = {
      message: "Expiration date successfully programmmed on device.",
    };

    return new Response(JSON.stringify(response), {
      status: 200,
      headers: {
        "Content-Type": "application/json",
      },
    });
  } catch (error) {
    return new Response(
      JSON.stringify({
        message: "Error storing expiration data",
        error: error.message,
      }),
      {
        status: 400, // Bad Request status
        headers: {
          "Content-Type": "application/json",
        },
      }
    );
  }
}
Was this page helpful?