wedmaniac
wedmaniac5d ago

Loop Detected in Deno Deploy

Hi there! I recently tried to deploy my WIP webpage until this point i have only hosted it locally, there everything has worked well so far! However now i get a error when fetching a API route when I use the deno.dev deployed page!
No description
8 Replies
wedmaniac
wedmaniacOP5d ago
below is my /api/get/modules/
import { Handlers } from "$fresh/server.ts";
import { createSupabaseClient } from "../../../plugins/auth.ts";
export const handler: Handlers = {
async GET(req, _ctx) {
const headers = new Headers();
const resp = new Response();
try {
const url = new URL(req.url); // Parse the URL

const pathId = url.searchParams.get("pathId");
console.log(pathId);

// Validate the header
if (!pathId) {
return new Response(
JSON.stringify({ success: false, message: "Path id header missing." }),
{ status: 400, headers }

);
}
const supabaseClient = createSupabaseClient(req, resp);

// Fetch modules for the given path slug
const { data: path, error: pathError } = await supabaseClient
.from("paths")
.select("id")
.eq("id", pathId)
.single();

if (pathError || !path) {
return new Response(
JSON.stringify({ success: false, message: "Path not found." }),
{ status: 404, headers }
);
}

const { data: modules, error: modulesError } = await supabaseClient
.from("modules")
.select("*")
.eq("path_id", path.id);

if (modulesError) {
console.error("Error fetching modules:", modulesError.message);
return new Response(
JSON.stringify({ success: false, message: "Failed to fetch modules." }),
{ status: 500, headers }
);
}

return new Response(
JSON.stringify({ success: true, modules }),
{ status: 200, headers }
);
}
.....
}
}
import { Handlers } from "$fresh/server.ts";
import { createSupabaseClient } from "../../../plugins/auth.ts";
export const handler: Handlers = {
async GET(req, _ctx) {
const headers = new Headers();
const resp = new Response();
try {
const url = new URL(req.url); // Parse the URL

const pathId = url.searchParams.get("pathId");
console.log(pathId);

// Validate the header
if (!pathId) {
return new Response(
JSON.stringify({ success: false, message: "Path id header missing." }),
{ status: 400, headers }

);
}
const supabaseClient = createSupabaseClient(req, resp);

// Fetch modules for the given path slug
const { data: path, error: pathError } = await supabaseClient
.from("paths")
.select("id")
.eq("id", pathId)
.single();

if (pathError || !path) {
return new Response(
JSON.stringify({ success: false, message: "Path not found." }),
{ status: 404, headers }
);
}

const { data: modules, error: modulesError } = await supabaseClient
.from("modules")
.select("*")
.eq("path_id", path.id);

if (modulesError) {
console.error("Error fetching modules:", modulesError.message);
return new Response(
JSON.stringify({ success: false, message: "Failed to fetch modules." }),
{ status: 500, headers }
);
}

return new Response(
JSON.stringify({ success: true, modules }),
{ status: 200, headers }
);
}
.....
}
}
Which I use in one of my routes: /paths/[slug].tsx
export default async function PathPage(req: Request, ctx: FreshContext<SignedInState>) {
const { session } = ctx.state;
const slug = ctx.params.slug;
let modules: Module[] = [];


try {
// Run both fetch requests in parallel
const [modulesResp, pathResp] = await Promise.all([
fetch(`https://economics.gg/api/get/modules?pathId=${slug}`, { redirect: "manual" }),
.....,
]);

// Handle modules response
if (modulesResp.ok) {
const data = await modulesResp.json();
modules = data.modules || []; // Set modules from response or empty array
} else {
console.error("Failed to fetch modules:", modulesResp.statusText);
}
} catch (error) {
console.error("Error fetching data:", error);
}
export default async function PathPage(req: Request, ctx: FreshContext<SignedInState>) {
const { session } = ctx.state;
const slug = ctx.params.slug;
let modules: Module[] = [];


try {
// Run both fetch requests in parallel
const [modulesResp, pathResp] = await Promise.all([
fetch(`https://economics.gg/api/get/modules?pathId=${slug}`, { redirect: "manual" }),
.....,
]);

// Handle modules response
if (modulesResp.ok) {
const data = await modulesResp.json();
modules = data.modules || []; // Set modules from response or empty array
} else {
console.error("Failed to fetch modules:", modulesResp.statusText);
}
} catch (error) {
console.error("Error fetching data:", error);
}
MaDsEn
MaDsEn4d ago
What does the error logs in deno deploy say?
ioB
ioB4d ago
You can't make a call to yourself. Instead of doing that, just call the function manually.
wedmaniac
wedmaniacOP4d ago
The error was the attached image. Not sure if you mean someting else:) Thansk for responding;) So I shouldn't do a fetch call at all? Should I have a helper function of some kind to retrieve the data, or should i just put the code in the /paths/[slug].tsx?
ioB
ioB4d ago
either one works
wedmaniac
wedmaniacOP4d ago
thanks a ton, will try them both and see which one i like most then;)
MaDsEn
MaDsEn4d ago
Misread thought the error came from when you deployed it to deno.deploy.
wedmaniac
wedmaniacOP4d ago
No worries! Thanks anyway:)