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

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

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 Report

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