1

I'm building a project with React & Redux & Firestore, I know how to delete/update/add etc.. But when I have 2 subcollections with dynamic field, I couldn't find any solution to delete it.

If you look at the picture, I have the table ID and user ID, but the fields are 0, 1, 2 and so on.

How can I delete a field from tableGuests? Maybe the structure is not good and could be better?

guests > user id > userTables > table id > tableGuests which is an array. enter image description here

danivegas
  • 487
  • 1
  • 5
  • 12

1 Answers1

1

How can I delete a field from tableGuests?

There is no clear way in the docs that explains how to pop an item from an array. I would do this:

  1. Fetch your table data with const data = firestore().collection('userTables').doc(ID).get();
  2. Use that data to get the current state of the array const array = data.get('tableGuests');
  3. Update document with a new array without the item you wish to remove (last one in this case) firestore().collection('userTables').doc(ID).update({ tableGuests: array.slice(0, array.length - 1) });
DoHn
  • 625
  • 7
  • 15
  • I know this is 1 week ago but thank, it worked :) Just step 3, I did with filter because it's not always the first one or last. – danivegas Mar 21 '19 at 11:25