Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use batch update in firestore web version-9?

In my nuxt project, I want to update some values in my firestore collection but i get mistakes

  const batch = writeBatch(firestore);

  const data = query(collection(firestore, `notifications`, `${uid}/news`));

  const querySnapshot = await getDocs(data);

  querySnapshot.forEach(doc => {
    if (doc.data().type === 'like') {
      batch.update(doc, { seen: true });
    }

     batch.commit();
  });

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You need to pass a DocumentReference as the first argument of the update() method, and not a QueryDocumentSnapshot.

You also need to commit the batch outside of the loop: you can commit the batch only once. This is what indicates the error message you added as a comment to your question.

Finally, note that you don’t need to use the query() method since you want to loop over the entire collection.


 const data = collection(firestore, `notifications`, `${uid}/news`);

 const querySnapshot = await getDocs(data);

 querySnapshot.forEach(doc => {
   if (doc.data().type === 'like') {
     batch.update(doc.ref, { seen: true });
   }
  });

  batch.commit();
 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading