1

I'm trying to fetch some coupons from my Next Api and here is the call:

export const getStaticProps = async () => {
  const requestOptions: any = {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
    },
  };

  try {
    const response = await fetch(
      `http://localhost:3000/api/coupon`,
      requestOptions
    );
    const data = await response.json();
    console.log('sada', data, response)

    return {
      props: {
        data
      }
    }
  } catch (error) {
    console.log(error);
  }
}

And here is the call in API:

const listCoupons = async (req: any, res: any) => {
  let response;

  const couponList = await Coupon.find({});

  if (couponList)
    response = {
      code: 200,
      success: true,
      message: `Coupons listed sucesfully.`,
      data: couponList,
    };
  else
    response = {
      code: 500,
      success: false,
      message: `Something wen't wrong while trying to list Coupons.`,
      data: null,
    };

  res.status(response.code).json(response);
};

Basiclly the API fetches data when I hit the endpoint of get in front and lists the coupons, but the static props doesn't fetch anything and the state is: this {}

bohokep350
  • 121
  • 10
  • You should not call an internal API route inside `getStaticProps`. Instead, you can safely use your API logic directly in `getStaticProps`. See [Fetch error when building Next.js static website in production](https://stackoverflow.com/a/66206394/1870780). – juliomalves May 21 '22 at 11:44

0 Answers0