I have a problem with Axios. I'm trying to nest multiple .then
and .get
.
I start a first get
to take back a JSON, then I get the ids from it. And for each of these ids, I make another get
to fill an array with a specific data. Unfortunately, I ended up with an empty array.
Here is my code
var axios = require('axios');
var config = {
method: 'get',
url: '.../api/firstCall',
headers: {
'Authorization': 'XXXXXXX',
'Cookie': 'XXXXXXXX'
}
};
axios(config)
.then(function (response) {
return (response.data.requests)
})
//Works fine (I Get the Ids for the second API call)
.then(function (response) {
const ids = []
Object.values(response).map(element => {
ids.push(element.id)
})
return ids
})
//For each Id I will make API call, get the data I want and put it in an array
.then(function (response) {
const theArrayToFill = []
response.map(id => {
var config2 = {
method: 'get',
url: '.../api/secondCall',
headers: {
'Authorization': 'XXXX',
'Cookie': 'XXXX'
}
};
axios(config2)
.then(function (response) {
const theData = response.data
theArrayToFill.push(theData)
})
.catch(function (error) {
console.log(error);
});
})
return theArrayToFill
})
// PROBLEM : my array is empty
.then(function (response) {
res.send(response)
})
.catch(function (error) {
console.log(error);
});
} ```