🏠 back to Observable

Issues with referencing array from API data

Hello,

I’m in the beginnings of a hobby-project using some data from the PUBG game API, and I’m running into a little trouble referencing some of the objects I’m pulling down from said API. See the attached image.

The API has an end point that generates a random list of over a thousands matches, which I’m successfully generating with this code.

I’m then successfully referencing the first five “match IDs” from that object and saving them as a new variable.

Then, I’m tapping the API again by passing that list of matches through a match-data endpoint of the API and getting five new objects back which each one’s match data.

Here’s where I’m running into issues. When I reference a specific value in the “matchDataA” array, I can see what’s inside that individual object.

But, when I try to reference a specific part of one of those values in the array, I get a return saying that the value is “undefined”… even though I can clearly see that the part of the object I’m referencing exists…

Any idea what I’m missing here? I’m assuming it has something to do with the fact that my array is displaying values as “promises”, but I haven’t quite figured out what that means yet.

This is my first foray on the Forums and my first Observable project overall, so I appreciate the help. :slightly_smiling_face:

Since matchData is an Array of Promises, you will need to use await or .then() to access the data they provide. Try:

(await matchData[0]).included

or

matchData[0].then(({included}) => included)
3 Likes

This was it! Thank you for helping me with such a basic thing, haha.

1 Like

You might want to look into Promise.all if you want to wait for every promise in that array to resolve.

3 Likes