Hola, esta es mi primera pregunta en stackoverflow, perdónenme si pregunto algo mal. Estoy hecho botón si hago clic en él eliminará la cantidad por uno. Entonces, después de mostrar eso en UI. Estoy enviando los datos al servidor. Pero cuando registro en la consola el req.body, se tarda entre 20 y 30 segundos en mostrarlo. A veces parece que no pasó nada. aquí está el código
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); });}, [artículo])
return( <MDBBtn className="w-100 my-5" color="danger" onClick={() => { handleDeliver(); }} > Delivered </MDBBtn>Este es el código en el lado del cliente:
app.put('/items/:id', async (req, res) => { console.log(req.body); res.status(500).send('testing'); })Pude resolver el problema cambiando el siguiente código:
setItem({ ...item, ...updateInfo});a
setItem(updateInfo);