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