I'm fetching data from backend, and I want to map everything. However I am struggling to render an array of objects . TypeError: Cannot read properties of undefined (reading 'cargoType')
Anyways, here's my code.. I tried to map it in a few different ways such as
const displayDashboard = bookings?.map((booking: BookingInterface, index) => (
<Text key={index}>{Object.values(booking?.pallets[index].cargoType)}</Text>
));
<Text key={index}>{booking.pallets.cargoType}</Text> (seems to return nothing) and a few other ideas that came to my mind like: booking.pallets[index].cargoType returns 'cannot read properties of undefined'. HOWEVER, I am unable to get any data, except if I do
Object.values(booking?.pallets[index] I receive an array of indexes like so: 01234
Here's how the data looks like (took out a few other entries besides 'pallets' to make it look cleaner):
0:
pallets: Array(5)
0: {cargoType: 'Engines & spare parts', stackable: false, cargoValue: '6092', count: '2', dangerous: false, …}
1: {cargoType: 'Furniture and accessories/Ceramics', stackable: false, cargoValue: '16485', count: '2', dangerous: false, …}
2: {cargoType: 'Engines & spare parts', stackable: false, cargoValue: '6879', count: '2', dangerous: false, …}
3: {cargoType: 'New Vehicles', stackable: false, cargoValue: '19980', count: '4', dangerous: false, …}
4: {cargoType: 'Industrial products', stackable: false, cargoValue: '9516', count: '2', dangerous: false, …}
length: 5
[[Prototype]]: Array(0)
1:
pallets: Array(4)
0: {cargoType: 'Chemicals & healthcare', stackable: false, cargoValue: '5741', count: '3', dangerous: false, …}
1: {cargoType: 'Old Vehicles', stackable: false, cargoValue: '18877', count: '2', dangerous: false, …}
2: {cargoType: 'Paperboard & prints', stackable: false, cargoValue: '6841', count: '1', dangerous: false, …}
3: {cargoType: 'Chemicals & healthcare', stackable: false, cargoValue: '7558', count: '1', dangerous: false, …}
length: 4
[[Prototype]]: Array(0)
Not really sure why isn't it rendering any data back. I was hoping to get it going with a simple booking.pallets.cargoType, but no - everything renders except this line. I believe that I am targeting the data improperly, but I ran out of ideas on how to get it to show up.
bookingsto referencebooking.pallets. What guarantee do you have thatpalletswill always have at least as many elements as thatbooking's index? For example, if thebookingat index 4 has apalletsarray with 3 elements, then clearlypallets[4]will fail. It's not really clear to me what you're expecting to output here.