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

217
Views
FIRESTORE: Chaining updates to modify array of objects does not work properly

I have an array in firebase called messages that stores objects that looks like:

{ 
   userId: userId,
   messageDocumentId: messageDocumentId,
   unreadMessages: 0,
}

I am trying to change the value of the unreadMessages field whose value I will use in a React component.

Since firestore does not support updating fields of an array element object, I tried to delete the object that is in the array for a corresponding userId and add another object with updated unreadMessages field value like:

  const resetMyMessageObject = async() => {
    if (messageDocumentId) {
      const messageObject = getSendersMessageObject();
      if (messageObject !== undefined) {
        const docRef = db.collection('users').doc(currentUser.uid);
        async docRef.update({
            messages: firebase.firestore.FieldValue.arrayUnion({
              userId: selectedUid,
              messageDocumentId: messageDocumentId,
              unreadMessages: 0,
            }),
          })
      
        async docRef.update({
            messages:
              firebase.firestore.FieldValue.arrayRemove(messageObject),
        });
         
      }
    }
  };

It does not add a document sometimes but will delete the old object in the array. I also tried chaining the update to delete in a .then after the update to add a new object in the array but it is not working as expected at all times. Could there be other ways to do what I am trying to achieve?

about 4 years ago · Juan Pablo Isaza
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!