GET Request
Unanswered
Mike posted this in #help-forum
MikeOP
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.
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
MikeOP
what should i use
fetch api
MikeOP
and why?
axios is unneeded
MikeOP
okay thanks
but this will change nothing on the request
MikeOP
i will answer tomorrow because i am in my bed right now oaky?
@!=tgt what's the contents
MikeOP
What did you mean with this? What the api route does?
like the code
MikeOP
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
MikeOP
This is my content
@!=tgt like the code
MikeOP
And the problem is that the request does not reach the api.
MikeOP
@!=tgt And?
Toyger
did you try to open url in browser directly? it should return response
MikeOP
Have fixed it