M Masood
M Masood

Reputation: 167

Getting a TypeError when using .map()

I have been trying to solve this bug for the past day and have not been able to, can someone help me out!

Problem: Getting TypeError: undefined is not object because of props.items.map...

Note: I am getting the value for 'props.items' from 'store/reducers/orders.js'. The dispatch to populate the state in 'store/reducers/orders.js' is called from 'screens/shop/CartScreen.js'. That is where the problem is but I do not see it.

props.items.map(cartItem => (
        <CartItem
          key={cartItem.productId}
          quantity={cartItem.quantity}
          amount={cartItem.sum}
          title={cartItem.productTitle}
        />

Path: components/shop/OrderItem.js

Link to project: https://codesandbox.io/s/github/SMasood1/Shop

Upvotes: 0

Views: 54

Answers (1)

Bassem
Bassem

Reputation: 4040

Make sure items exist before you run .map():

props.items && props.items.map(cartItem => (
        <CartItem
          key={cartItem.productId}
          quantity={cartItem.quantity}
          amount={cartItem.sum}
          title={cartItem.productTitle}
        />

Upvotes: 2

Related Questions