Hello This is my first question in stackoverflow please forgive me if I ask something wrong. I'm made button if I click it it will delete quantity by one. So after showing that on UI. I'm sending the data to server. But when I console log the req.body it taking 20-30s to show it. Sometimes it looks likes nothing happend. here is the code
const handleDeliver = () => {
const newQuantity = parseInt(item?.quantity) - 1;
const newSold = parseInt(item?.sold) 1;
if (newQuantity >= 0) {
const updateInfo = {
quantity: newQuantity "",
sold: newSold "",
};
setItem({
...item,
...updateInfo,
});
} else {
toast.warning("Please Restock the Car.");
}
};
useEffect(() => {
fetch(`http://localhost:5000/items/${id}`, {
method: "PUT", // or 'PUT'
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
quantity: item?.quantity,
sold: item?.sold,
}),
})
.then((response) => response.json())
.then((data) => {
toast("Success:", data);
});
}, [item])
return(
<MDBBtn
className="w-100 my-5"
color="danger"
onClick={() => {
handleDeliver();
}}
>
Delivered
</MDBBtn>
This is the code on client side:
app.put('/items/:id', async (req, res) => {
console.log(req.body);
res.status(500).send('testing');
})
CodePudding user response:
I was able to resolve the issue by changing the following code:
setItem({
...item,
...updateInfo});
to
setItem(updateInfo);