'I want to decrease my stock amount by the delivery button in MongoDb

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



Solution 1:[1]

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) })

Solution 2:[2]

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);
    })
        
    
};

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sheikh Mahmudul Hasan
Solution 2 Parvez Islam