Get object array from axios and using return value call another axios function and append the output to first result
const request = { headers: { "Accept": "application", "Content-Type": "application/json", }, method: "GET", url: '', } as any;
const result = await axios(request);
result.data.data.map(async (ad: any) => {
const request2 = {
headers: {
"Accept": "application",
"Content-Type": "application/json",
},
method: "GET",
url: '',
} as any;
const result2 = await axios(request2);
ad.preview = result2.data.data[0].body;
})
return result.data.data;
preview key is not getting appended
Solution 1:
The problem is that the return-statement runs before the async map-callbacks have finished. Since you're not actually returning anything inside your map, it does not make a lot of sense to use it though. Just use a for..of
loop:
const result = await axios(request);
for (const ad of result.data.data) {
const request2 = {...}
const result2 = await axios(request2);
ad.preview = result2.data.data[0].body;
}
return result.data.data;