#Prisma confusing error message

11 messages · Page 1 of 1 (latest)

strange dirge
#

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

white ridgeBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

strange dirge
#
  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);
    }
  }```
```ts
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?
}
balmy flare
#

In your api route, do

req.json() instead of req.body

strange dirge
#

Is it ok if it would be of type any?

balmy flare
#

Replace nextapirequest with just Request

strange dirge
#

Ah yeah this seemed to have worked

#

Thanks