0

I want to add an Object to an existing Array in firestore. I already have one Object in the Array, now I want to add the second Object.

So I have created this code below to store data to firestore. After I triggered the function the data in firestore won't add a new Object to the array

Code:

  let docId = `${this.currentUser.uid}`

  fb.usersCollection.doc(docId).update({
      userId: this.currentUser.uid,
      posts: [
        {
          createdOn: this.postDetails.createdOn,
          content: this.postDetails.content,
          image: this.postDetails.image,
          comments: this.postDetails.comments,
          likes: this.postDetails.likes,
          tags: this.model,
          userData: [

            { userName: this.userProfile.name,
              userId: this.currentUser.uid,
              userImage: this.userProfile.userImage
            }
          ]
        }
      ]
    })
Lukas
  • 573
  • 2
  • 8
  • 19

1 Answers1

0

Update doesn't automatically add a new object to array, the way you are doing it, you are simply overwriting the posts Array with a new object.

To add new object to existing array, you either read old array and then manipulate it your self, then write it back in using the transaction method or use the new array method as mentioned by @Joseph

Linh
  • 418
  • 4
  • 9