Home > Blockchain >  Axios returning empty on get call for nested data [duplicate]
Axios returning empty on get call for nested data [duplicate]

Time:10-07

I am trying to get a 2d array using axios but when i console.log it it returns empty :/

My code:

let orig = []
      axios
        .get(<endpoint url here>)
        .then(response => {
          orig = response.data.activity_history
        })

      console.log('Orig -> '   JSON.stringify(orig))

My endpoint is built to return data like this:

{
    "id": 1,
    ...
    "activity_history": [
        [
            "Test",
            "Test",
            "Test",
            "Test",
            "Test"
        ]
    ]
}

I'm trying to get the 2d array so I could push another array in it in the frontend, but when i console.log orig it returns Orig -> []. Any help?

CodePudding user response:

because the console.log doesnt wait for the axios call to finish. One option would be to add await to your axios. Other option would be to do all actions with your Orig in the then function like:

  axios
        .get(<endpoint url here>)
        .then(response => {
          orig = response.data.activity_history;
          console.log('Orig -> '   JSON.stringify(orig))
        })
  • Related