Next.js Discord

Discord Forum

500 error when sending data from client side component, to app/api/products/route.ts

Answered
Waterman posted this in #help-forum
Open in Discord
I do not use pages diretory, to get the data I use app/api/products/route.ts
and i am sending from my client side component thats in app/(pages)/produkter/ny/page.ts

First Question
is my setup in route.ts correct or should I use NextResponse insead of res?
Second question - answered
also is my folder structure valid to be able to reach products?



Code Below


My app/api/products/route.ts:

export const POST = (req: any, res: any) => {
  if (req.method === "POST") {
    // Handle POST request
    console.log(req);

    res.json(req.method);
  }
};

My app/(pages)/produkter/ny/page.ts:

"use client";

import BaseLayout from "@/components/baselayout";
import { useState } from "react";
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>
      "...more code but not necessary for this question I don't think"
Answered by joulev
export const POST = (req: Request) => {
  console.log(req);
  return NextResponse.json({ method: req.method });
};
View full answer

6 Replies

error I get when using res(all the code linked above)
@Waterman I do not use pages diretory, to get the data I use app/api/products/route.ts and i am sending from my client side component thats in app/(pages)/produkter/ny/page.ts **First Question** is my setup in route.ts correct or should I use NextResponse insead of res? **Second question - answered** also is my folder structure valid to be able to reach products? **Code Below** My app/api/products/route.ts: javascript export const POST = (req: any, res: any) => { if (req.method === "POST") { // Handle POST request console.log(req); res.json(req.method); } }; My app/(pages)/produkter/ny/page.ts: javascript "use client"; import BaseLayout from "@/components/baselayout"; import { useState } from "react"; 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> "...more code but not necessary for this question I don't think"
is my setup in route.ts correct
no it is not correct
should I use NextResponse insead of res?
in route handlers, you must use Response or an extension of it (e.g. NextResponse)
also is my folder structure valid to be able to reach products?
app/api/products/route.ts is correct, app/(pages)/produkter/ny/page.ts you probably want to name it in .tsx extension
ok thanks for structured answer, could you maybe try to provide me with a short correct setup, have sat for hours trying to figure out how to do it using app direcotyr
would be very nice if you could
@Waterman ok thanks for structured answer, could you maybe try to provide me with a short correct setup, have sat for hours trying to figure out how to do it using app direcotyr
export const POST = (req: Request) => {
  console.log(req);
  return NextResponse.json({ method: req.method });
};
Answer
can't thank you enough, very nice of you, everything works