当我在网站应用程序上使用 API 时,它工作得很好,但是当我尝试在 Postman 中测试它时,我收到 SyntaxError: No number after minus sign in JSON atposition 1 (line 1 column 2)。我已将数据放入 Postman 的正文中,并确保名称正确,但错误仍然存在。奇怪的是,我只能通过 POST 请求得到这个。我的 GET 和 DELETE 请求在 Postman 中工作正常。从网站将产品添加到购物车后的 JSON 响应是:
{ 编号:70, 产品编号:21, 用户ID: 10, 尺寸:“中号”, 客户数量:1 }
但是当我使用邮递员将所需的数据放入正文时,它会在标题中给出错误
API代码:
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();
}
}
您没有显示您在邮递员正文中放入的具体内容,但如果您放入所谓的“JSON 响应”,则会出现错误,因为它不是 JSON。它是一个 Javscript 对象,但不是有效的 JSON。
您需要更改您的:
{ id: 70, ProductId: 21, userId: 10, 尺寸: 'Medium', customerQuantity: 1 }
到
{ "id": 70, "productId": 21, "userId": 10, "size": 'Medium', "customerQuantity": 1 }