0

What is the way to remove an item in an index from array of map in firestore?

I have an array of maps as in the picture I attached, I need to delete an index of array 'cart'.

I tried arrayRemove() but its not working.enter image description here

1
  • 3
    can you share some code? like this admin.firestore.FieldValue.arrayRemove({ count:'3', item_id:'123' }) Commented Jun 16, 2020 at 11:04

2 Answers 2

2

You cannot delete an array item using its index. This is intentional because, there may be multiple clients adding/removing the items in the array, which will change the indexes. Hence, you must delete the items by values. More details here.

Furthermore, if you need conditional delete, like "delete all items with count 1", then you need to handle this case yourself, on the client side as: get the array, update it, push it back.

Sign up to request clarification or add additional context in comments.

Comments

0

Depending on your requirements, if you can change your data structure to a map instead of an array then you can delete at a specific key.

cart: {
  0: {count: 3...},
  1: {count: 1...} 
}

Then on your client (details here):

docRef.update.{['chat.' + index]: firebase.firestore.FieldValue.delete()};

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.