Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

94
Vistas
Update one property in MongoDB and update UI without refresh the page

I am trying to update one property Like I have a few properties, product name, price, quantity, supplier, and description. I am sending all the updated quantities with all properties to MongoDb, in that case, I am able to update the database and UI without any refresh.

const handleDelivered = (id) => {
        
        const newQuantity = (quantity - 1);
        if (newQuantity >= 0) {
            const newService = {...serviceDetail, quantity: newQuantity}
            setServiceDetail(newService);
            const url = `https://intense-tor-77999.herokuapp.com/item/${id}`;

            fetch(url, {
                method: 'PUT',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify(newService),
            })
                .then(response => response.json())
                .then(data => {
                    console.log('Success:', data);                
                })
                .catch((error) => {
                    console.error('Error:', error);
                }) 
        }
        else{
            toast(`${productName} is sold out`);
        }
    } 

But I just want to update only one property for example only quantity like

const updateQuantity = { quantity : newQuantity};

So, how can I update this one property without sending all properties from my frontend to backend?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Firstly you can declare const [isReload, setIsReload]= useState(true);

when you get response from database after uploading new quantity, add this

.then(response => response.json())
            .then(data => {
                console.log('Success:', data); 
                  setIsReload(!isReload);               
            });

than you put isReload as dependency of useEffect, where you load data by id

    useEffect(()=>{
       fetch(url)
        .then(res=>res.json())
      .then(data => setData(data))
    },[isReload ]);

try it

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda