Home > Blockchain >  I want to decrease my stock amount by the delivery button in MongoDb
I want to decrease my stock amount by the delivery button in MongoDb

Time:05-09

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

CodePudding user response:

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

CodePudding user response:

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