Running my API in a website application works flawlessly, but encountering SyntaxError when testing it in Postman - specifically "No number after minus sign in JSON at position 1" (line 1 column 2). The data is correctly inputted into the body of Postman and the names are accurate, yet the error persists. Strangely enough, this issue only occurs with POST requests; GET and DELETE requests function properly in Postman. The JSON response post adding a product to the basket from the website reads as follows:
{ id: 70, productId: 21, userId: 10, size: 'Medium', customerQuantity: 1 }
However, when attempting to replicate the process in Postman by providing the necessary data in the body, the error mentioned in the title arises.
The API code:
import prisma from "@/app/prismadb";
import { NextResponse } from "next/server";
export async function POST(request: Request) {
const body = await request.json();
const { productId, userId, size, customerQuantity } = body;
try {
const existingCartItem = await prisma.basket.findFirst({
where: {
productId,
userId,
size: {
contains: size,
}
},
});
if (existingCartItem) {
await prisma.basket.delete({
where: {
id: existingCartItem.id,
},
});
}
const product = await prisma.basket.create({
data: {
productId,
userId,
size,
customerQuantity
},
});
return NextResponse.json(product);
} catch (error) {
console.log("Error adding product to cart", error);
return NextResponse.error();
}
}
export async function DELETE(request: Request) {
const body = await request.json();
const { productId, userId } = body;
try {
const deleteItem = await prisma.basket.deleteMany({
where: {
productId: productId,
userId: userId,
},
});
return NextResponse.json(deleteItem);
} catch (error) {
console.log("Error deleting product", error);
return NextResponse.error();
}
}