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

259
Vistas
How to add a new value to a Firestore field in a document in addition to the previous value?

I want to create a function that checks if a collection in Firestore exists and if it exist I want it to add the value given by the user to the value stored in the document inside the collection

My approach:

  items.forEach(item=>
  
      User &&  db.collection("users").doc(Uid).collection("basket").where( "id" , "==" , item.id)
      .get().then(function(querySnapshot) {
        if (querySnapshot.empty) {
          
                 // DO SOMETHING

        }
         else {
         
          db.collection("users").doc(Uid).collection("basket").where("id" == item.id)
          .get().then((docs) => {
        
            docs.forEach((doc) =>{ doc.ref.set({
              quantity : firebase.firestore.FieldValue.increment(item.quantity)
            })}).then(setLocked(true))
          
           
        })
               
        }
    })
    )

however it shows the following error :

Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'ut')

I doubled check the code and it turns out that the error comes from the beginning of the query the else statement however there is nothing wrong with it

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

0

As Stf_F commented, you can either pass merge: true to the set call:

docs.forEach((doc) =>{ 
  doc.ref.set({
    quantity : firebase.firestore.FieldValue.increment(item.quantity)
  }, { merge: true }) // 👈
})

or you can use update:

docs.forEach((doc) =>{ 
  doc.ref.update({ // 👈
    quantity : firebase.firestore.FieldValue.increment(item.quantity)
  })
})

The .then(setLocked(true)) makes no sense, since forEach doesn't return a promise.

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