Search code examples
reactjsarraysapiobject

I'm having issues accessing data after fetch


I fetched data from an API and I get this : An array of arrays containing objects and properties containing the details of songs.

enter image description here

The issue is when i try to access the uri property, I get "Cannot read properties of undefined (reading '1')" with the code below :

 const songTitleEl = chartData.map(data =>(
    <ChartSongs key={data.key} audio={data?.hub?.actions[1]?.uri}/>
  ))

It should be working perfectly but it isn't and for the life of me I can't figure why.


Solution

  • actions could be null. Need the ? to check if index exists

    <ChartSongs key={data.key} audio={data?.hub?.actions?.[1]?.uri}/>