-2

I am trying to access the "players" array coming from this API i've called in axios, the return looks like this when I am printing response.data:

enter image description here

Now, I thought it would be easy to access it, so I tried(without success):

response.data.players (print out all players)
response.data.players[0] (print out first player)
response.data[0] (maybe get the array?)

What am I doing wrong?

  • Please provide more information, do you get an error - if so, whats the error? – Damian Busz Jun 12 '22 at 16:44
  • When trying to console.log some of these: "TypeError: response.data[0] is undefined" – QuestionableCodingPractices Jun 12 '22 at 16:45
  • did you try response.players? – Dmitriy Zhiganov Jun 12 '22 at 16:46
  • There's no `data` property in your screenshot. There **is** a `response` property in your screenshot. So it's likely that you need a `response` property in your access expression, probably `theVariableYouLogged.response.players` (which is an array). See also https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json, which this is a duplicate of (I misvoted earlier). – T.J. Crowder Jun 12 '22 at 16:46
  • Hi, I'm using response.data because it returns the body of what we are calling (https://masteringjs.io/tutorials/axios/data) – QuestionableCodingPractices Jun 12 '22 at 16:49

1 Answers1

1

I have a feeling that response.data (based on the screenshot you've attached for response.data) has one more property called response inside it, which has players array. Maybe try using response.data.response.players[0].

Let me know if that doesn't work so I can make necessary changes to the answer :)

Harsh Tuwar
  • 281
  • 2
  • 9
  • This was the answer! Was confused why it was providing "response" inside of my data. – QuestionableCodingPractices Jun 12 '22 at 16:50
  • Hi @QuestionableCodingPractices, if this or any answer has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. Thanks :) – Harsh Tuwar Jun 12 '22 at 16:57