Cant make an api request inside of getServerSideprops
Unanswered
Spectacled bear posted this in #help-forum
Spectacled bearOP
I cant make an api request to the route using axios because I got an error.
export async function getStaticProps() {
const reponse = axios.get("/api/getUserById");
const { data } = await reponse;
console.log(data);
}//api route
import { supabase } from "@/config/supabase";
export default async function handler(req: any, res: any) {
if (req.method !== "GET") {
return res.status(405).json({ error: "Method not allowed" });
}
if (req.query.API_ROUTE_SECRET !== process.env.NEXT_PUBLIC_API_ROUTE_SECRET) {
return res.status(401).json({ error: "Unauthorized" });
}
try {
const { data, error } = await supabase.from("profile").select("*").single();
console.log(req.body);
if (error) {
res.status(500).json({ error: error.message });
} else {
res.status(200).json({ data });
}
// Validate the email address (You can add more validation here if needed)
} catch (error) {
console.error("Error subscribing:", error);
return res.status(500).json({ error: "Failed to subscribe" });
}
}3 Replies
Giant panda
You're not supposed to fetch your own endpoints in server-side code.
Instead extract the logic into a function that you call from getStaticProps etc. directly
Near is right, you can't fetch your own endpoints in server-side code, because they do not exists during deployment.