Next.js Discord

Discord Forum

GET Request

Unanswered
Mike posted this in #help-forum
Open in Discord
Hello guys, i have this api folder structure for my request.

api/profile/getAbsences/[userId]/route.jsx

I am trying to send an get request like this. But i can't receive any response on my api.

  try {
    const { data } = await axios.get("/api/profile/getAbsences/" + session?.user._id);
    return data.absences;
  } catch (error) {
    console.error("Error fetching absences:", error);
    throw error;
  }

21 Replies

what should i use
fetch api
and why?
axios is unneeded
okay thanks
but this will change nothing on the request
ok i was just saying
what's the contents
of the api route?
i will answer tomorrow because i am in my bed right now oaky?
@!=tgt what's the contents
What did you mean with this? What the api route does?
like the code
import { getServerSession } from "next-auth/next";
import { nextAuthOptions } from "../../../auth/[...nextauth]/route";
import { dbConnect, dbDisconnect } from "@/utils/database";
import Absence from "@/models/Absence";

const handler = async (request) => {

  console.log("request");

  // Überprüfe die Sitzung
  const session = await getServerSession(nextAuthOptions);
  if (!session) {
    return Response.json(
      {
        message: "You are not authenticated",
      },
      {
        status: 401,
      }
    );
  }

  const { userId } = request.query;

  console.log("current userId", userId);

  const id = "6624435a12f04660e996fb4e";

  console.log(session?.user);

  try {
    // Verbinde mit der Datenbank
    await dbConnect();

    const absences = await Absence.find({id: id});

    return Response.json(
      {
        absences: absences, 
      },
      {
        status: 200,
      }
    );
  } catch (error) {
    console.error("Fehler beim Verarbeiten des Requests:", error);
    return Response.json(
      {
        message: "Internal Server Error",
      },
      {
        status: 500,
      }
    );
  } finally {
    await dbDisconnect();
  }
};

export { handler as GET };
@!=tgt yeah
This is my content
@!=tgt like the code
And the problem is that the request does not reach the api.
@!=tgt And?
Toyger
did you try to open url in browser directly? it should return response
Have fixed it