-1
I’m trying to create a front end based on the Wars star API, my plan was to create an object to then show on the screen. When I make a request(https://swapi.co/films for example), some object properties come as an array of links. So I thought of calling function inside the request to make other requests using AXIOS, but when I return the data, they all come back together. How can I make object properties receive data correctly?
My code goes below:
const urlAPI = 'https://swapi.co/api/'
async function getInformation(arrayURL) {
arrayData = []
promisses = []
arrayURL.forEach(element => {
console.log(element)
const promisse = axios(element)
.then(response => response)
.then(resp => arrayData.push(resp.name))
promisses.push(promisse)
})
axios.all(promisses)
console.log(arrayData)
return await arrayData;
}
axios.get(`${urlAPI}films`)
.then(response => {
return (response.data.results)
})
.then(resp => {
// console.log(resp)
resp.forEach(element => {
if (element.hasOwnProperty('planets')) {
element.planets = getInformation(element.planets)
console.log(element.planets)
}
})
})
.then(data => {
console.log(data)
})
.catch(error => {
console.log(error)
})
What do you mean "they all come back together"? You can give an example?
– Sam
By code, each set of requests I call through the getInformation function should return the values to the element.Planets of each element, however it only returns to the last element and all together in a disorganized way.
– Carlos
On each console.log, it should be showing the names of the planets I called the getInformation function, but it shows empty. Only the last one he shows.
– Carlos