1

I'm trying to update a field in a firestore array.

I have a document in a collection that goes like this:

started: agoust,
ends: november,
positions: [
{
player: 'user2',
points: 6
},
{
player: 'user3',
points: 3
},
{
player: 'user4',
points: 0
},
{
player: 'user1',
points: 1
},
]

I want to update only the points of the current user (user1) and i cant find the answer.

Ive tried something like this:

await firebase
      .firestore()
      .collection('tournaments')
      .doc(bet?.id)
      .update({
        positions: firebase.firestore.FieldValue.arrayUnion({
          player: user?.username,
          points: 0
        })
      })

This doesnt works, it clears all the array

  • arrayUnion can only add items to an array (if they don't already exist). To modify an array item, you have to read the document, modify the array in memory, then write it back. – Doug Stevenson Sep 21 '20 at 19:13
  • thanks, and how can i do that? –  Sep 21 '20 at 19:15
  • First, click through to the duplicates that I marked in this question. If you're stuck on any of these three steps, post a new question explaining where you're stuck, along with the code that doesn't work the way you expect. The three steps I propose are each very easy and the product documentation explains them in detail. – Doug Stevenson Sep 21 '20 at 19:21

0 Answers0