Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

192
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!