Reputation: 35
I encounter an issue when I send a post request with JSON content to Next.js API route handlers using thunder client
{
"category_name": "Category Name"
}
When sending a POST request using Thunder Client or in the frontend, i get 500 Internal Server Error with this code
Invalid `prisma.category.create()` invocation:
{
data: {
+ category_name: String
}
}
Argument `category_name` is missing.
i use nextjs api Route Handlers to post Category Name
//api/category/route.js
import prisma from "../../../../lib/prisma";
import { NextResponse } from "next/server";
export async function POST(request){
const {category_name} = request.body;
const userCategory = await prisma.category.create({
data:{
category_name: category_name
}
})
const data = await userCategory.json();
return NextResponse.json(data);
}
and here is the prisma schema
generator client {
provider = "prisma-client-js"
previewFeatures = ["jsonProtocol"]
}
datasource db {
provider = "postgresql"
url = env("POSTGRES_PRISMA_URL") // uses connection pooling
directUrl = env("POSTGRES_URL_NON_POOLING") // uses a direct connection
}
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_id], references: [id])
category_id Int
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[]
}
The issue I am facing is that I can’t send a post request to the database to save the category name or the product info
Upvotes: 1
Views: 826
Reputation: 49571
this is what req.body
(property) Body.body: ReadableStream<Uint8Array> | null
use req.json
in try/catch
export async function POST(request) {
try {
const { category_name } = await request.json();
const userCategory = await prisma.category.create({
data: {
category_name: category_name,
},
});
const data = await userCategory.json();
return NextResponse.json(data);
} catch (error) {
console.error("eror in post", error);
}
}
Upvotes: 1