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

417
Vistas
How to update field in firebase document?

Hello I want to update a field in a firebase document.

Right now , I am accessing the document collection like this However I am having trouble setting teh shares field of the doc to shares+=1 ? What am i doing wrong here in the set method?

const buyStock = async () => {
const q = await query(
  collection(db, 'myStocks'),
  where('ticker', '==', props.name)
);
const querySnapshot = await getDocs(q);
if (!querySnapshot.empty) {
  // simply update the record

  querySnapshot.forEach((doc) => {
    doc.id.set({
      shares: doc.id.data().shares+=1
    })
    // doc.data() is never undefined for query doc snapshots
    console.log(doc, doc.id, ' => ', doc.data());
  });
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You're almost there. To update a document, you need a reference to that document. You can get this with:

querySnapshot.forEach((doc) => {
  doc.ref.update({
    shares: doc.data().shares+=1
  })
});

You can also use the built-in atomic increment operator:

doc.ref.update({
  shares: increment(1)
})
about 4 years ago · Juan Pablo Isaza Denunciar

0

It looks like you are calling the set method on the 'id' field. Try to remove that. So just do

doc.set({
  shares: doc.data().shares+=1
})

give that a shot. If that doesn't work, i'd try updating a document that is not part of a query snapshot.

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