0

I'm trying to increment the fields max_invite and s_max_invite on the structure below:

enter image description here

firestore.collection('groups').doc(doc.id).update({
        'types.0.max_invite': firestore.FieldValue.increment(1),
        'types.0.s_max_invite': firestore.FieldValue.increment(1),
    })

}

The result is:

enter image description here

What is wrong?

Thanks

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
genericUser
  • 4,417
  • 1
  • 28
  • 73

1 Answers1

0

Firebase do not support a direct update of a specific index of array. So I had to read the document, extract the data and update locally the array index. Then update the document to firestore again.

function updateArrayIncreament(myDoc) {
    let docData = _.cloneDeep(myDoc.data())
    docData.types[0].max_invite += 1
    docData.types[0].s_max_invite += 1
    return docData
}

firestore.collection('groups').doc(doc.id).update(
    updateArrayIncreament(doc)
)
genericUser
  • 4,417
  • 1
  • 28
  • 73