TypeError: Cannot read properties of undefined (reading 'json')
Answered
Waterman posted this in #help-forum
WatermanOP
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:
api/products/page.ts:
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 / Basproduktapi/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
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
route.ts file in your app folder2. 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
31 Replies
WatermanOP
I am using the app dir
WatermanOP
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?WatermanOP
sorry I mean pages directory
well then that depends on you
do you want to use the
pages directory or app directoryyou can combine the two
WatermanOP
I prefer app
but just tell me which file is in app or in pages or include them in your filepath
WatermanOP
I send from (pages)/basprodukter/ny/page.tsx
@alfon but just tell me which file is in app or in pages or include them in your filepath
WatermanOP
is that what you asked for?
1. to create an API in your nextjs application, you do it by creating a
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
route.ts file in your app folder2. 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
WatermanOP
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
WatermanOP
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
WatermanOP
ok I think I can solve it with this help, thanks alot 🙂
no problem