i’m trying to solve a problem in which i have to use a find() method and it’s giving me an error
import { CART_ADD_ITEM, CART_REMOVE_ITEM } from '../constants/cartConstant';
const cartReducers = (state = { cartItems: [] }, action) => {
switch (action.type) {
case CART_ADD_ITEM:
const item = action.payload;
const existItem = state.cartItems.find((x) => x.product === item.product);
if (existItem) {
return {
...state,
cartItems: state.cartItems.map((x) =>
x.product === existItem.product ? item : x
),
};
} else {
return { ...state, cartItems: [...state.cartItems, item] };
}
default:
return state;
}
};
export default cartReducers;
This is my cartReducer.js file and i get an error in this file, when switching to a case ‘CART_ADD_ITEM’
the error looks like this piece of code below
caught (in promise) TypeError: Cannot read properties of undefined (reading 'find')
at cartReducers (cartReducers.js:8:1)
at combination (redux.js:560:1)
at E (<anonymous>:1:33326)
at I (<anonymous>:1:33613)
at <anonymous>:1:37893
at Object.dispatch (redux.js:288:1)
at e (<anonymous>:1:38904)
at index.js:20:1
at dispatch (redux.js:691:1)
at cartAction.js:7:1
c