How do i decrease stock in my react app by a delivery button which is connected in MongoDB
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); }) };
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) })
Click here to cancel reply.
2
Answers
const delevary = e => {
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)
})