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

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

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 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!