Array containing http.get results becomes undefined

Asked 2 years ago, Updated 2 years ago, 99 views

I want to put the values I got by hitting the API in the array and take them out at any time.

 const http=axios.create({
    baseURL: 'base-url',
})

vara = [ ]

function addArray() {
  http.get('/hoge/fuga', {data:{}}.then(response=>{
    a. push (response.data.name)
  })
}

Array.from({length:30}.map()=>{
    addArray()
    console.log(a[0])
  })

Some source codes are omitted, but console.log(a[0]) means undefined.

console.log(a) will look like the following, and I think I can get it with index:0 but I can't.

 [ ]
    0: 'Name'
    length —0
    __proto —Array(0)
        concat —f concat()
        It's long, so I'll skip it.

Why can't I get it?

javascript axios

2022-09-30 11:03

1 Answers

I don't think I can get it.

When you run console.log(a[0]),

.then(response=>{
    a. push (response.data.name)
})

Isn't it just that the callback has not been called and the value has not been set yet?


2022-09-30 11:03

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.