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 {}