0
0 Comments

I would like to be able to reduce the stock size of a product after a purchase has been confirmed. I’m using the deliverOrderHandler as the trigger since an admin needs to manually press to confirm delivery.

I’ve created a function called reduceStockHandler that will remove one item from the countInStock size however, I am having issues updating the stock even by one. This is in the OrderScreen.js and I used ProductEdit.js as a template since that is a section that already updates the stock amount of a product.

Error:  Cannot read properties of undefined (reading ‘user’)

const { id: productId } = useState('');
  const [user, setUser] = useState('');
  const [price, setPrice] = useState('');
  const [isPaid, setisPaid] = useState('');
  const [isDelivered, setisDelivered] = useState('');
  const [countInStock, setCountInStock] = useState('');
   useEffect(() => {
    const fetchData = async () => {
      try {
        dispatch({ type: 'FETCH_REQUEST' });
        order.orderItems?.map((item) => {
          const { data } = axios.get(`/api/products/${parseInt(item._id)}`);
          setUser(data.user);
          setPrice(data.totalPrice);
          setisPaid(data.isPaid);
          setisDelivered(data.isDelivered);
          setCountInStock(data.countInStock);
          dispatch({ type: 'FETCH_SUCCESS' });
        });
      } catch (err) {
        dispatch({
          type: 'FETCH_FAIL',
          payload: getError(err),
        });
      }
    };
    fetchData();
  }, [productId]);
   const reduceStockHandler = async (e) => {
    e.preventDefault();
     // Reduce stock of item by 1
    setCountInStock(setCountInStock - 1);
     try {
      dispatch({ type: 'UPDATE_REQUEST' });
      order.orderItems?.map((item) => {
        axios.put(
          `/api/products/${parseInt(item._id.id)}`,
          {
            _id: parseInt(item._id.id),
            user,
            price,
            isPaid,
            isDelivered,
            countInStock,
          },
          {
            headers: { Authorization: `Bearer ${userInfo.token}` },
          }
        );
      });
      dispatch({
        type: 'UPDATE_SUCCESS',
      });
      toast.success('Product updated successfully');
    } catch (err) {
      toast.error(getError(err));
      dispatch({ type: 'UPDATE_FAIL' });
    }
  };

Thank you for your time,

Khalid

Basir Answered question October 11, 2022