P
Prisma2w ago
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",
},
}
);
}
}
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",
},
}
);
}
}
2 Replies
Prisma AI Help
You chose to compile with the bot. One moment while I piece together your answer… Based on the provided code and the information in the knowledge sources, there are a few potential issues that could be causing the 400 response code in your NextJS API route. Let's address them: 1. Prisma Client Initialization: The code snippet doesn't show how the Prisma client is initialized. Make sure you're properly initializing and importing the Prisma client. You can refer to the Handling exceptions and errors documentation for proper setup. 2. Asynchronous Operation: The findDevice operation is not awaited, which means it's returning a Promise instead of the actual count. Change this line:
const findDevice = prisma.device.count({

const findDevice = prisma.device.count({

to:
const findDevice = await prisma.device.count({

const findDevice = await prisma.device.count({

3. Comparison: After fixing the above, change the condition to check if the count is greater than 0:
if (findDevice > 0) {

if (findDevice > 0) {

4. Error Handling: Your catch block is returning a 400 status code for all errors. This might be masking the actual error. Consider logging the full error object:
console.error(error);

console.error(error);

This will help you identify if there's a specific Prisma error occurring. 5. Database Connection: Ensure that your database connection is properly configured. The error might be related to a connection issue, especially if it's working locally but not in production. Double-check your environment variables and database URL. 6. Prisma Schema: Make sure your Prisma schema correctly defines the Device model with the fields uuid, product, and lastUpdated. If you're still encountering issues after making these changes, you might want to add more detailed error logging or use Prisma's error types to handle specific errors. For example:
} catch (error) {
console.error(error);
if (error instanceof Prisma.Prism
aClientKnownRequestError) {
// Handle known Prisma errors
console.log(error.code);
}
return new Response(
JSON.stringify({
message: "Error storing expiration data",
error: error.message,
}),
{
status: 500, // Internal Server Error might be more appropriate
headers: {
"Content-Type": "application/json",
},
}
);
}
} catch (error) {
console.error(error);
if (error instanceof Prisma.Prism
aClientKnownRequestError) {
// Handle known Prisma errors
console.log(error.code);
}
return new Response(
JSON.stringify({
message: "Error storing expiration data",
error: error.message,
}),
{
status: 500, // Internal Server Error might be more appropriate
headers: {
"Content-Type": "application/json",
},
}
);
}
For more information on handling Prisma errors, refer to the Handling exceptions and errors documentation. 🤔 Alright, thanks for letting us know that there's still some help needed. A team member will reply to you shortly. In the meantime, please let us know any additional info, questions, or which parts of the answer may need clarification.
Nurul
Nurul2w ago
Hey 👋 Is the device record created in the database after "Test 2" is logged or you directly get a 400 error without a record being created.

Did you find this page helpful?