Search code examples
angulargoogle-cloud-firestoreangularfire2

Delete an element with index in an array-CloudFirestore


I am trying to delete an element that's inside an array. Below is the structure of my collection

Items:
-----subGroupId
---------------[0]
------------------itemName
------------------itemPrice
---------------[1]
------------------itemName
------------------itemPrice
---------------[2]
------------------itemName
------------------itemPrice

Before deleting i am experimenting to get the item at a particular index. But i am getting "undefined" when logged.

this.db.collection<any>("items").doc(this.subGroupData.sgId).collection("items").doc("/0").valueChanges().subscribe(data=>{
              console.log(data);
            })

I expect the output be an object of the item data.

please help. Thanks in advance.


Solution

  • I think you might be mixing up subcollections with array fields. They are not the same thing.

    If you have a field of a document which is an array, you can't treat it like a subcollection. The only way to delete an array field element by index is by reading the document, modifying the array in memory, and updating the array field back to the document.