I want to fetch data with API. Fecth
is my utils, the get API data. It working the only array so I used the fetch function in the javascript the sample object below. My utils is not supported object.
The problem I'm having is not related to utils. The problem is array push
in async function.
Array push is not working the map function outside :(
But, is working in the map function :)
What should I do to run outside?
/* eslint-disable */
import Fetch from '@/utils/fetch';
async function allPosts(req, res) {
const myPostsAll = await Fetch(`${process.env.NEXT_PUBLIC_API_URL}/${process.env.NEXT_PUBLIC_API_USERNAME}`);
const posts = [];
myPostsAll.map(async (item) => {
const { id } = item;
const myPostRes = await fetch(`${process.env.API_URL}/${id}`);
const myPost = await myPostRes.json();
const { title, description, cover_image, body_html } = myPost;
posts.push({
id: id.toString(),
title,
thumbnail: cover_image,
description,
content: body_html,
});
// It's work
console.log(posts);
});
// It's not work
console.log(posts);
}