未处理的拒绝(类型错误):无法读取未定义的属性“查找”

时间:2021-02-15 14:48:57

标签: reactjs react-redux react-router

当我刷新它显示的页面时,由于查找而出现错误,然后错误无法读取属性“查找”出现。我有什么地方做错了吗?

from my cartAction.js 这是我用来发送动作的

xport const addToCart = (id, qty) => async (dispatch, getState) => {
const { data } = await axios.get(`/api/products/${id}`)

dispatch({
    type: CART_ADD_ITEM,
    payload: {
        product:data._id,
        name:data.name,
        image:data.image,
        price:data.price,
        countInStock:data.countInStock,
        qty
    }
})
localStorage.setItem('cartItems', JSON.stringify(getState().cart.cartItems))

}

export const cartReducer = (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]
                    }
                }
    
            case CART_REMOVE_ITEM:
                return {
                    ...state,
                    cartItems: state.cartItems.filter(x => x.product !== action.payload)
                } 
            default:
                return state
        }
    }

0 个答案:

没有答案