-1

I'm trying to acess the first element of this array but every time returns me undefined

error

I'm trying to acess the first element of this array but every time returns me undefined I used damageRelations[0] but it's not working and i don't know what to do.

    types.map((type) => {
        console.log(type.url)
        fetch(type.url)
            .then((response) => response.json())
            .then((data) => {damageRelations.push(data)})
        })
        console.log(damageRelations[0])

The error on console

mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • 1
    Fetch is async, you need to wait for it to finish. Right now you console log `damageRelations[0]` before fetch has even done anything. Also, as shown the function you passed into `map` doesn't return anything, so you're not mapping anything: (a) please [edit] your post and update the code so it shows a complete `.map()` call, and (b) what is this code supposed to do? – Mike 'Pomax' Kamermans Sep 02 '23 at 16:19

0 Answers0