-1

I have declared three arrays dronesList, ndzDrones, pilotsList. After all the conditions are executed, I am getting updated data of dronesList and ndzDrones but the pilotsList is empty, although I'm receiving the data and pilotsList is updated inside the condition after pushing but outside of condition getting Empty[] of pilotsList. Can't figure what I'm missing here. Any help will be appriciated

let dronesList = []
    let ndzDrones = []
    
    try {
        const xml = await fetch(
            "https://assignments.reaktor.com/birdnest/drones"
          ).then((res) => res.text())
          .catch((error) => {
            console.error('Error:', error)
          })
        const jsonData = parser.parse(xml)
        const dronesData = jsonData.report.capture.drone;
        dronesList = dronesData;

        if(dronesList !== ""){
            
            dronesList.map((drone) =>{
                const x = Number(drone.positionX)
                const y = Number(drone.positionY)
                if(checkVoilation(x, y)){
                    ndzDrones = ndzDrones.concat(drone)
                }
            })
        }

        let pilotsList = []
        if(ndzDrones !== ""){
            ndzDrones.forEach(async (drone) => {
                const serial = drone.serialNumber
                const response = await fetch(`https://assignments.reaktor.com/birdnest/pilots/${serial}`)
                const data = await response.json()
                pilotsList = pilotsList.concat(data)
            })
        }
        console.log(dronesList) //Output [drones]
        console.log(ndzDrones) //Output [ndzDrones]
        console.log(pilotsList) //Output [] Empty Array
        res.status(200).json(pilotsList)
    } catch (error) {
        res.status(404).json({message: error.message})
    }
tsm009
  • 39
  • 1
  • 9
  • Please provide an idea of what the data looks like after it comes back the fetch call. As a matter of fact, that would be a good way for *you* to debug this issue, just just some good old-fashioned hard-coded data as part of your process toward a solution. – Dexygen Dec 31 '22 at 00:00
  • @Dexygen after **concat** pilotsList inside the condition is like this ```[ { pilotId: 'P-bDDYUSvaMO', firstName: 'Tate', lastName: 'Beatty', phoneNumber: '+210445710227', createdDt: '2022-11-18T04:42:18.294Z', email: 'tate.beatty@example.com' } ]``` While outside condition its empty – tsm009 Dec 31 '22 at 00:03
  • 1
    @DreamBold already using async/await. – tsm009 Dec 31 '22 at 00:06
  • https://codesandbox.io/s/suspicious-sanderson-uhw80i?file=/src/App.js Please have a look at this. can you replicate your issue here? – DreamBold Dec 31 '22 at 00:09
  • 1
    [This answer](https://stackoverflow.com/a/37576787/13561410) may be helpful. – Oskar Grosser Dec 31 '22 at 11:21

1 Answers1

1
const pilotsList = await Promise.all(ndzDrones.map(async (drone) => {
  const serial = drone.serialNumber
  const response = await fetch(`https://assignments.reaktor.com/birdnest/pilots/${serial}`)
  const data = await response.json()
  return data
})).flat()
Konrad
  • 21,590
  • 4
  • 28
  • 64