我想使用prisma和nextjs Route Handlers API更新数据库中的单个记录
import prisma from "../../../../lib/prisma";
import { NextResponse } from "next/server";
export async function POST(req) {
try {
const { quantity, product_name } = req.json();
const productUpdate = await prisma.products.update({
where: {
product_name: product_name,
},
data: {
quantity: quantity,
},
});
const data = JSON.stringify(productUpdate);
return NextResponse.json(data);
} catch (error) {
console.error("Error:", error);
return NextResponse.error(
"An error occurred while processing the request."
);
}
}
当我发送JSON内容时
{
"quantity": 10,
"product_name": "alfa Pen"
}
我得到这个错误
Error: PrismaClientValidationError:
Invalid `prisma.products.update()` invocation:
{
where: {
product_name: undefined,
? id?: Int
},
data: {
quantity: undefined
}
}
Argument `where` of type ProductsWhereUniqueInput needs at least one argument. Available options are listed in green.
我也尝试更新多个记录
import prisma from "../../../../lib/prisma";
import { NextResponse } from "next/server";
export async function POST(req) {
try {
const { quantity, product_name ,price } = req.json();
const productUpdate = await prisma.products.updateMany({
where: {product_name:{
contains: product_name,
}
},
data: {
quantity: quantity,
price: price,
},
});
const data = JSON.stringify(productUpdate);
return NextResponse.json(data);
} catch (error) {
console.error("Error:", error);
return NextResponse.error(
"An error occurred while processing the request."
);
}
}
我来吧
"{\"count\":0}"
这是prisma模式
generator client {
provider = "prisma-client-js"
previewFeatures = ["jsonProtocol", "fullTextSearch", "fullTextIndex"]
}
datasource db {
provider = "postgresql"
url = env("POSTGRES_PRISMA_URL") // uses connection pooling
directUrl = env("POSTGRES_URL_NON_POOLING") // uses a direct connection
shadowDatabaseUrl = env("POSTGRES_URL_NON_POOLING") // used for migrations
}
model Products {
id Int @id @unique @default(dbgenerated("floor(random() * (9999 - 1000 + 1)) + 1000"))
product_name String @unique @db.VarChar(50)
product_description String @db.Text
quantity Int @db.Integer
price Decimal @db.Decimal()
pproduct Category @relation(fields: [category_name], references: [category_name])
category_name String
productsOrder_id Int
ProductsOrder ProductsOrder[]
}
model Category {
id Int @id @unique @default(dbgenerated("floor(random() * (9999 - 1000 + 1)) + 1000"))
category_name String @unique @db.VarChar(50)
products Products[]
}
model ProductsOrder {
id Int @id @unique @default(dbgenerated("floor(random() * (9999 - 1000 + 1)) + 1000"))
date DateTime @default(now())
quantity Int @db.Integer
totalAmount Decimal @db.Decimal()
products Products[]
}
model Service {
id Int @id @unique @default(dbgenerated("floor(random() * (9999 - 1000 + 1)) + 1000"))
serviceName String @db.VarChar(50)
price Decimal @db.Decimal()
ServiceOrder ServiceOrder[]
}
model ServiceOrder {
id Int @id @unique @default(dbgenerated("floor(random() * (9999 - 1000 + 1)) + 1000"))
quantity Int @db.Integer
totalAmount Decimal @db.Decimal()
services Service[]
}
我也检查错误的变量或不存在的重新排序,但没有什么是丢失或错误的变量在数据库
1条答案
按热度按时间gpnt7bae1#
在req.json后添加wait