'How do I update a record using the put API?
As you can see in the React Js code I am trying the Reduce one by one quantity when clicking the Delivered button. When the Click Delivered button quantity becomes null.
const reduceQuantity=(productId)=>{
const quantityReduce=parseInt(product.quantity )- 1
const url = `http://localhost:5000/product/${productId}`
fetch(url, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ quantityReduce }),
})
.then((res) => res.json())
.then((data) => {
console.log('success', data)
});
return (
<div className='item-container'>
<div className='text-center'>
<img src={product.picture} alt="" />
</div>
<h6><b>{product.name}</b></h6>
<p className='text-justify'><b>Description:</b>{product.Description}</p>
<div className='d-flex'>
<div>
<p><b>Price:</b>{product.price}</p>
<p><b>supplierName:</b>{product.supplierName}</p>
</div>
<p><b>Quantity:</b>{product.quantity}</p>
</div>
</div>`enter code here`
<div className='text-center d-flex justify-content-around'>
<button className='btn btn-outline-success rounded-pill' >Delivered</button>
server Side API:
// update quantity of products
app.put("/product/:id", async (req, res) => {
const id = req.params.id;
const data = req.body;
console.log("from update api", data);
const filter = { _id: ObjectId(id) };
const options = { upsert: true };
const updateDoc = {
$set: {
quantity: data.addQuantity
},
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
