问题
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.
回答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:
- Fetch your table data with
const data = firestore().collection('userTables').doc(ID).get(); - Use that data to get the current state of the array
const array = data.get('tableGuests'); - 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) });
来源:https://stackoverflow.com/questions/55101973/delete-data-from-firestore-dynamically