Prisma confusing error message
Answered
Pavement ant posted this in #help-forum
Pavement antOP
I'm trying to build a simple form where a user can input a title and some content to go with it, However prisma is logging that title isn't present, however in my code it clearly is present, I've even logged the body of the request and title and content are parsed with the text i put in the inputs so generally i'm very confused why this is happening, Its also logging a 500 internal server error
9 Replies
Pavement antOP
async function handleSubmit(e: React.FormEvent) {
e.preventDefault();
console.log(title, content);
try {
await fetch("/api/create-post/", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ title, content }),
});
} catch (err) {
console.error(err);
}
}import prisma from "../../../lib/prisma";
import { NextApiRequest } from "next";
import { NextResponse } from "next/server";
export async function POST(req: NextApiRequest) {
const response = await req.body;
const { title, content } = response;
console.log({ response });
const result = await prisma.post.create({
data: {
title: title,
content: content,
published: true,
},
});
return NextResponse.json({ result });
}generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("POSTGRES_PRISMA_URL") // uses connection pooling
directUrl = env("POSTGRES_URL_NON_POOLING") // uses a direct connection
}
model Post {
id String @default(cuid()) @id
title String
content String?
published Boolean @default(false)
author User? @relation(fields: [authorId], references: [id])
authorId String?
}In your api route, do
req.json() instead of req.body
req.json() instead of req.body
Answer
Pavement antOP
Is it ok if it would be of type any?
Replace nextapirequest with just Request
Pavement antOP
Ah yeah this seemed to have worked
Thanks