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

193
Visualizações
How to increment a data field in Firestore using Firebase Modular SDK V9?

So I'm trying to increment data for sending a num to other user's num.

const sendNum = async(e) => {
        e.preventDefault();
        
        
        const targetQuery = query(userCol, where("keycode", "==", parseInt(target)));
        const targetSnapshot = await getDocs(targetQuery)
        
        targetSnapshot.forEach((document) => {
            console.log(document.data().num)
            console.log(document.id)
            const targetRef = doc(db, 'users', document.id)

            setDoc(targetRef, {
                num: // increment user data here
            }, {merge: true})
        })
}

But a documentation that I found, I don't really understand them. And the one I understand, it's already outdated. So I would be very happy if anyone know how :)

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

0

The documentation has a dedicated section for that called increment a numeric value.

import { setDoc, increment } from "firebase/firestore";

await setDoc(targetRef, {
    num: increment(50)
});

You cannot use await in a forEach loop (setDoc returns a promise and must be awaited) so try using either Batch Writes or Promise.all().

Using async/await with a forEach loop

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