I'm working on a project where I am trying to have my cart saved to the database (mongodb) while it's in use. I'm getting an error stating
Cannot use 'in' operator to search for 'validateStatus' in 62d61b450a76374089e56b75 (id).
Originally, I was sending a put request, but even when I changed it to a post request after seeing examples advising that with this error you need to change it to a post request, I'm still getting the same error. Does anyone know why this is happening?
cartActions.js
export const addInPreOrderCart = (userCartId, productId, pre_orderQty) => async (dispatch, getState) => {
dispatch({ type: CART_ADD_PREORDER_REQUEST, payload: userCartId, productId, pre_orderQty });
try {
const { data } = await Axios.put(`${BASE_URL}/api/carts/pre_orderQty`,
userCartId,
productId,
pre_orderQty, {
});
dispatch({ type: CART_ADD_PREORDER_SUCCESS, payload: data });
} catch (error) {
dispatch({
type: CART_ADD_PREORDER_FAIL,
payload: error.response && error.response.data.message ? error.response.data.message : error.message,
});
}
};
cartReducers.js
export const cartAddPreOrderReducer = (state = {}, action) => {
switch (action.type) {
case CART_ADD_PREORDER_REQUEST:
return { ...state, loading: true };
case CART_ADD_PREORDER_SUCCESS:
return { ...state, loading: false, success: true, cart: action.payload };
case CART_ADD_PREORDER_FAIL:
return { ...state, loading: false, error: action.payload };
case CART_ADD_PREORDER_RESET:
return {};
default:
return state;
}
};
cartRouter.js
cartRouter.put(
'/pre_orderQty',
expressAsyncHandler(async (req, res) => {
console.log(req.body)
const cart = await Cart.findOne({ userCartId: req.body.userCartId});
if (cart.cartItems.find((x) => x.productId === req.body.productId)) {
let carT = cart.cartItems
for (var items of carT) {
if (items.productId === req.body.productId) {
items.pre_orderQty = req.body.pre_orderQty;
}
}
const updatedCart = await cart.save();
res.send({ message: 'Cart Item Updated', cart: updatedCart });
} else {
res.status(404).send({ message: 'Cart Not Found' });
}
}),
);