你好這是我在stackoverflow中的第一個問題,如果我問錯了,請原諒我。如果我點擊它,我會按按鈕洗掉數量。所以在UI上顯示之后。我正在將資料發送到服務器。但是當我控制臺記錄 req.body 時,它需要 20-30 秒才能顯示出來。有時看起來好像什么都沒發生。這是代碼
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);
});
}, [物品])
return(
<MDBBtn
className="w-100 my-5"
color="danger"
onClick={() => {
handleDeliver();
}}
>
Delivered
</MDBBtn>
這是客戶端的代碼:
app.put('/items/:id', async (req, res) => {
console.log(req.body);
res.status(500).send('testing');
})
uj5u.com熱心網友回復:
我能夠通過更改以下代碼來解決該問題:
setItem({
...item,
...updateInfo});
到
setItem(updateInfo);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/469117.html
標籤:javascript 节点.js 反应 表示
