I have a question how can I deal with fetching through loop in getStaticProps, because when I do that the array is empty. It is caused by asynchronous code. If I pass ID array and put logic into react component it works fine. Should I stick with component approach or do it in getStaticProps. If getStaticProps how can I pass data?
export const getStaticProps: GetStaticProps = async (context) => {
const assetsId = await getDataFromMongo();
const assetsArray = [] as Asset[];
console.log(assetsId);
assetsId.forEach((asset) => {
const getAssets = async () => {
const response = await fetch(
`https://rest.coinapi.io/v1/assets/${asset}`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
"X-CoinAPI-Key": "API KEY",
},
}
);
const data = await response.json();
assetsArray.push(data);
};
getAssets();
});
console.log(assetsArray);
return {
props: {
assets: assetsId,
},
revalidate: 1,
};
};