Next.js Discord

Discord Forum

TypeError: Cannot read properties of undefined (reading 'json')

Answered
Waterman posted this in #help-forum
Open in Discord
Hello, I get errors when trying to send data through post request, I get 200 response but can't retrieve the preview in chrome webtools due to the json error.

I appreciate all help 🙂

Error can be seen in one of the linked images


Code below

file I am sending from:
import axios from "axios";

export default function page() {
  const [namn, setNamn] = useState("");
  const [fulltNamn, setFulltNamn] = useState("");
  const [beskrivning, setBeskrivning] = useState("");
  const [historia, setHistoria] = useState("");
  const [kategori, setKategori] = useState("");
  const [pris, setPris] = useState("");

  async function createProduct(e: any): Promise<void> {
    e.preventDefault();
    const data = { namn, fulltNamn, beskrivning, historia, kategori, pris };
    await axios.post("/api/products", data);
  }

  return (
    <BaseLayout>
      <form onSubmit={createProduct} className="w-full text-start p-4">
        <h1 className="text-white text-2xl py-8 font-extrabold">
          Ny BasTavla / Basprodukt


api/products/page.ts:
export default function handle(req: any, res: any): void {
  console.log(req);

  res.json(req.method);
}
Answered by alfon
1. to create an API in your nextjs application, you do it by creating a route.ts file in your app folder
2. you don't need to fetch your own API
all page component are server component by default. which means that you can just write the back end logic inside the page async component
View full answer

31 Replies

I am using the app dir
this is the wrong way to use API handler in nextjs app directory
try reading the docs again
so I then gotta create the pages app directory? or just do it differently when using app?
what do you mean by pages app directory?
sorry I mean pages directory
well then that depends on you
do you want to use the pages directory or app directory
you can combine the two
I prefer app
but just tell me which file is in app or in pages or include them in your filepath
I send from (pages)/basprodukter/ny/page.tsx
i meant this
you didn't specify whether this is in app or pages
anyway
1. to create an API in your nextjs application, you do it by creating a route.ts file in your app folder
2. you don't need to fetch your own API
all page component are server component by default. which means that you can just write the back end logic inside the page async component
Answer
so I should never have to create routes by making folders?
when getting requests?
If you call request on your own Front end server component, then no
if you want to receive request OUTSIDE of nextjs application OR from a client component then you can create API
but if I send something to /api/products like I did in the code, will that really be correct to put a route.tsx in my app direcotry?
yes thats correct
but its just redundant
because you are calling your own server
browser request data to server, then server request data to server then server respond back to browser
ok I think I can solve it with this help, thanks alot 🙂
no problem