Next.js Discord

Discord Forum

data fetching

Answered
astro posted this in #help-forum
Open in Discord
im trying to fetch data from my api but the data doesnt show up, even in the network tab no request is being made to the api, here's my code:

component:
import axios from "axios";
import React from "react";

export default function OrdersPage() {
  async function getOrders() {
    try {
      const res = await axios.get("/api/orders");
      const orders = await res.data;

      return JSON.stringify(orders);
    } catch (error) {
      console.log(error);
    }
  }

  return <pre>{getOrders()}</pre>;
}


api route
export async function GET() {
  return NextResponse.json({ message: "Hello!" }, { status: 200 });
}

41 Replies

try to call the method outside of the dom and also await it. lmk if that works
@astro how outside the dom?
red is inside the dom and yellow is outside the dom. Put your function in the yellow part
@B33fb0n3 red is inside the dom and yellow is outside the dom. Put your function in the yellow part
where do u even see the red and yellow? 😭
@astro where do u even see the red and yellow? 😭
whoops forgot the screenshot. Place it at the blue line
and don't forget to add the await and read the result. And then show the result here
Horned Puffin
Maybe something with the cache of nextjs
Answer
@B33fb0n3 whoops forgot the screenshot. Place it at the blue line
i migrated to fetch api btw
import React from "react";

export default async function OrdersPage() {
  async function getOrders() {
    const res = await fetch("/api/orders");
    if (!res.ok) {
      return { message: "Cannot fetch data" };
    }

    const orders = await res.json();

    return orders;
  }

  console.log(await getOrders());

  return <pre>{}</pre>;
}
if i remove the async/await from the component it doesnt thorw error
nor do i get any output
@joulev .
but the route handler sends a rewuest to an external api if i do it on client side its gonna throw CORS error, plus api authorisation will also causr an issue
@B33fb0n3 It seems like you are on server side. So just fetch the external api here
u mean call the external api inside the component?
oh wait it actually worked
import React from "react";
import { generateAccessToken } from "@/lib/authorisation";

export default function OrdersPage() {
  async function getOrders() {
    const accessToken = await generateAccessToken(
      process.env?.QIKINK_CLIENT_ID ?? "",
      process.env?.QIKINK_CLIENT_SECRET ?? ""
    );
    const res = await fetch("https://api.qikink.com/api/order", {
      headers: {
        ClientId: process.env?.QIKINK_CLIENT_ID ?? "",
        Accesstoken: accessToken,
      },
    });
    if (!res.ok) {
      return JSON.stringify(await res.json(), null, 2);
    }
    return JSON.stringify(await res.json(), null, 2);
  }

  return <pre>{getOrders()}</pre>;
}
i have a few doubts tho
when i tried calling the api directly inside another component(client component) it threw CORS error
the getOrders function returns a promise, even tho i aint resolving it inside the pre tag it gives me the data, how?
@astro the getOrders function returns a promise, even tho i aint resolving it inside the `pre` tag it gives me the data, how?
Now please bring some vision in this code like this:
import React from "react";
import { generateAccessToken } from "@/lib/authorisation";

export default function OrdersPage() {
  const accessToken = await generateAccessToken(
      process.env?.QIKINK_CLIENT_ID ?? "",
      process.env?.QIKINK_CLIENT_SECRET ?? ""
  );
  const res = await fetch("https://api.qikink.com/api/order", {
      headers: {
        ClientId: process.env?.QIKINK_CLIENT_ID ?? "",
        Accesstoken: accessToken,
      },
  });

  const orders = await res.json();
  console.log(orders) // <--- whats this value?
  return <pre>{JSON.stringify(orders, null, 2)}</pre>;
}

And also add the await, the result and the console.log and say, what's the response.
@astro hello?
@B33fb0n3 <@776348276802584576> hello?
it returns the same output
@astro it returns the same output
// <--- whats this value?
@B33fb0n3 > // <--- whats this value?
its the json data from API
either give me the result or I will leave this thread. wtf
@B33fb0n3 either give me the result or I will leave this thread. wtf
im not allowed to share it
it's my company's data
lmao
how we gotta help you then?
just share some piece of data
problems don't get magically solved 🪄
@averydelusionalperson how we gotta help you then?
You speak from my soul. You deserve the unblock ❤️
sadge
not anymore ^^
@astro its the json data from API
wait... when I think about it.. this thread is solved... you had problems to get the data and now you get the data... solved 👍
i just wanted to know why it didnt throw CORS error