skip to Main Content

How do i decrease stock in my react app by a delivery button which is connected in MongoDB

2

Answers


  1. const delevary = e => {

        const quantity = product?.quantity
        const updateItem = {quantity}
        const url = `http://localhost:7000//${id}`;
        fetch(url,{
            method : 'PUT',
            headers : {
                'content-type' : 'application/json'
            },
            body : JSON.stringify(updateItem)
        })
        .then(res => res.json())
        .then (result =>{
            console.log(result)
            setIsreload(!isReload)
            // setItems(result);
        })
            
        
    };
    
    Login or Signup to reply.
  2. If you click on the Delivered button, you will reduce the quantity one by one, then you will send that value to the backend and update it in the database.
    Increase in the same way.

    as like…

    const stockQuantity = parseInt(products?.quantity)
    const newStockQuantity = stockQuantity – 1;

    then

    fetch(url, {
    method: ‘PUT’, // or ‘PUT’
    headers: {
    ‘Content-Type’: ‘application/json’,
    },
    body: JSON.stringify(
    {
    quantity: newStockQuantity,
    }
    ),
    })
    .then(response => response.json())
    .then(data => {
    console.log(‘Success:’, data);
    toast(‘Your Product Deliver successfully’)
    setIsReload(!reload)
    })

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search