在进一步深入研究之前,以下是我正在使用的技术的简要概述:
我是 prisma 的新手,当我尝试在 Prisma 中更新嵌套数据时,它总是在 catch 块中返回错误,并显示如下错误消息:
TypeError: Cannot read properties of undefined (reading 'map') at PATCH ...
我很确定这个补丁部分有错误,但我不知道它在哪里,这是代码:
export const PATCH = async (
req: NextRequest,
{ params }: { params: { id: string } }
) => {
try {
const { id } = params
const body: ProductPostProps = await req.json()
const { title, price, description, stock, categories, images } = body
const updateProduct = await prisma.product.update({
where: {
id,
},
data: {
title,
price,
description,
stock,
categories: {
create: categories.map(({ title }) => ({
title,
})),
},
images: {
create: images.map(({ image }) => ({
image,
})),
},
},
include: {
images: true,
categories: true,
},
})
if (!updateProduct) {
return NextResponse.json({
message: "data not found",
status: 404,
})
}
return NextResponse.json({
message: "product updated",
})
} catch (error) {
//error appear in this section
console.log(error)
return NextResponse.json({
message: "internal server error",
error,
status: 500,
})
}
}
当我使用 POST 方法发布嵌套数据时,它工作得很好。这是一个例子:
export const POST = async (req: NextRequest) => {
try {
const body: ProductPostProps = await req.json()
const { title, price, description, stock, categories, images } = body
await prisma.product.create({
data: {
title,
price,
description,
stock,
categories: {
create: categories.map(({ title }) => ({
title,
})),
},
images: {
create: images.map(({ image }) => ({
image,
})),
},
},
include: {
images: true,
categories: true,
},
})
return NextResponse.json({
message: "product successfully created",
status: 201,
})
} catch (error) {
console.log(error)
return NextResponse.json({
message: "internal server error",
error: error,
status: 500,
})
}
}
这是成功发布后数据的示例:
{
"id": "662672ede82c9e68a76f9b06",
"title": "Nike Dunk Low Retro SE",
"price": 1900000,
"description": "The '80s b-ball icon returns with a classic construction and throwback hoops flair. Channelling vintage style back onto the streets, its padded, low-cut collar lets you take your game anywhere—in comfort.",
"stock": 10,
"createdAt": "2024-04-22T14:23:41.713Z",
"updatedAt": "2024-04-22T14:23:41.713Z",
"images": [
{
"id": "662672ede82c9e68a76f9b0a",
"image": "https://static.nike.com/a/images/t_PDP_1728_v1/f_auto,q_auto:eco/6464f9e4-59af-4179-9c7f-e81d334db2aa/dunk-low-retro-se-shoes-tsMmHC.png",
"productId": "662672ede82c9e68a76f9b06"
},
{
"id": "662672ede82c9e68a76f9b0b",
"image": "https://static.nike.com/a/images/t_PDP_1728_v1/f_auto,q_auto:eco/49f166a8-7ea5-4b64-b4a3-05fb4d420281/dunk-low-retro-se-shoes-tsMmHC.png",
"productId": "662672ede82c9e68a76f9b06"
},
],
"categories": [
{
"id": "662672ede82c9e68a76f9b07",
"title": "men's shoes",
"productId": "662672ede82c9e68a76f9b06"
},
{
"id": "662672ede82c9e68a76f9b09",
"title": "nike",
"productId": "662672ede82c9e68a76f9b06"
}
],
},
这是我的 Prisma 数据模型:
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}
model Product {
id String @id @default(auto()) @map("_id") @db.ObjectId
title String
price Int
images ImageProduct[]
description String?
stock Int?
categories Category[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@map("products")
}
model ImageProduct {
id String @id @default(auto()) @map("_id") @db.ObjectId
image String?
product Product @relation(fields: [productId], references: [id], onDelete: Cascade)
productId String
@@map("image_products")
}
model Category {
id String @id @default(auto()) @map("_id") @db.ObjectId
title String
product Product @relation(fields: [productId], references: [id], onDelete: Cascade)
productId String
@@map("categories")
}
这不是 Prisma 错误,而是传入数据的错误。
就像错误消息所说,
categories
或images
是undefined
,这就是为什么当您尝试映射它时会抛出异常。
我会记录请求正文的值,并尝试找出客户端未在其 PATCH 请求中发送预期数据的原因。