Next.js Discord

Discord Forum

Possible fetch bug?

Unanswered
In&Out posted this in #help-forum
Open in Discord
import { createClient } from "@/utils/supabase/server";
import styles from "./_components/Main.module.css";

async function fetchFromFastAPI() {
  try {
    const response = await fetch("http://127.0.0.1:8000", {
      method: "GET",
      headers: {
        "Cache-Control": "no-cache", // Ensure that the response is not cached
      },
    });
    console.log("Response status:", response.status); // Log status
    console.log("Response headers:", response.headers); // Log headers
    const data = await response.json();
    console.log("Fetched data:", data); // Debugging log
    return data;
  } catch (error) {
    console.error("Error fetching data from FastAPI:", error);
    return null;
  }
}

export default async function Page() {
  let apiData;
  try {
    apiData = await fetchFromFastAPI();
  } catch (error) {
    console.error("Error fetching data from FastAPI:", error);
  }

  const supabase = createClient();
  const { data: supabaseNotes } = await supabase.from("test").select();

  return (
    <div className={styles.container}>
      <div className={styles.apiDataContainer}>
        {apiData ? (
          <div>{apiData.hello}</div>
        ) : (
          <div>Failed to fetch FastAPI data</div>
        )}
      </div>
      <div className={styles.notesContainer}>
        {supabaseNotes?.map((note, i) => (
          <div key={i} className={styles.note}>
            {note.test}
          </div>
        ))}
      </div>
    </div>
  );
}

10 Replies

You can ignore the supabase part, so my issue is, i am messing with nextjs and fastapi, when i fetch from
http://127.0.0.1:8000
, i should get {"hello":"blop1231234"}, but i get a value that i had like 10 minutes ago, even when i shut down caching, why?
even removing cookies and pressing ctrl + f5 doesnt work
what i get when fetching that route:
Response status: 200
Response headers: Headers {
  'content-length': '16',
  'content-type': 'application/json',
  date: 'Mon, 12 Aug 2024 18:23:06 GMT',
  server: 'uvicorn'
}
Fetched data: { hello: 'blop' }
it only works when i change caching, but gotta change type every time i change data
okay, seems i got it working with this
    const response = await fetch("http://localhost:8000", {
      cache: "no-cache",
    });
But i wonder why is this happening, and will this mess up my fetching with backend?
fetching with postman or thunder client gives no issue
you can try this
alright thanks