Next.js Discord

Discord Forum

Fetching doesn't work after build

Unanswered
domestos posted this in #help-forum
Open in Discord
I have an API route where i do my DB connection and then get and return neccessary data.

route.ts

export const GET = async (req: Request) => { const INCREMENT_LIMIT_VALUE = 6; try { connectToDb(); const { searchParams } = new URL(req.url); const limit = searchParams.get("limit") ?? INCREMENT_LIMIT_VALUE; const coatsItems = await Product.find().limit(+limit); const totalCoatsItemCount = await Product.countDocuments(); return NextResponse.json({ coatsItems, totalCoatsItemCount }); } catch (error) { return NextResponse.json({ error: "Failed to fetch data" }); } };


Also i have separated function just to make my component clean, and the page component itself where i use this function.

fetchProducts.ts

export async function fetchProducts({ limit }: IParams): Promise<{ coatsItems: IProduct[]; totalCoatsItemCount: number; }> { const res = await fetch(${process.env.API_URL}/availability?limit=${limit}, { cache: "no-store", } ); if (!res.ok) throw new Error("Failed to fetch data"); const data: { coatsItems: IProduct[]; totalCoatsItemCount: number } = await res.json(); return data; }

page.tsx

const { coatsItems, totalCoatsItemCount } = await fetchProducts({ limit: currentLimit.toString(), });

Everything works in development but i can't get the data after build , I understand that this might seem like a trivial issue, but I'm still unable to figure out why it's happening.

5 Replies

you shldnt fetch your own api, it will cause errors like you are facing right now
@domestos
@Arinji <@968862484394029126>
Thank you for sharing this link. I'll read it and try. :meow_coffee:
Sure, mark it as the soln if it soled your query :D