Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

172
Visualizações
How to reduce quantity by 1 from mongodb and client side by clicking button using node js?
const ProductEdit = () => {
  const[user]=useAuthState(auth);

  const {inventoryId}=useParams();
  const [product,SetProduct]=useState({});
  useEffect(()=>{
    const url=`http://localhost:5000/product/${inventoryId}`;
    fetch(url)
    .then(res=>res.json())
    .then(data=>SetProduct(data));
  },[])
  return (
    <div className='mt-5 '>
       <h3 className='text-center'> Product Detail of {product.name}</h3>
       <form className='w-50 mx-auto'>
         <input className='w-100 mb-2' type="text" placeholder='Name' name='name' value={product.name} required/>
         <br />
         <input className='w-100 mb-2' type="text" placeholder='Price' name='quantity' value={product.price} required/>
         <br />
         <input className='w-100 mb-2' type="text" placeholder='Quantity' name='number' value={product.quantity} required/>
         <br />
         <input onclick={handleReduce} type="submit" value="Delivered" />
        
       </form>

I want to reduce the amount of the quantity from the data which is loaded also in MongoDB. what should do?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I see what you are trying to do.

You can do this in a couple of ways.

1st method is that you reduce the quantity from the server side by just sending -1 in the body.

2nd method is that you do your calculation from the client-side and send the updated quantity to the server-side.

In any case, you can do this with a function.

I see that you are calling a function on the onClick event.

So, why not use that?

All you have to do is wrap that onClick function as a callback function. Just like this:

<input onclick={()=>handleReduce(-1)} type="submit" value="Delivered" />

See? I just pass -1 as the function parameter.

And you can declare the function like this:

const handleQuantity = (number) => {
    axios.patch(
      `http://localhost:5000/items/${id}`,
      {
        quantity: item.quantity + number,
      },
      {
        headers: {
          Authorization: `Bearer ${localStorage.getItem("token")}`,
        },
      }
    );
    const newItem = { ...item, quantity: item.quantity + number };
    setItem(newItem);
  };
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda