Delete data from Firestore dynamically

女生的网名这么多〃 提交于 2020-07-19 18:59:06

问题


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:

  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) });


来源:https://stackoverflow.com/questions/55101973/delete-data-from-firestore-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!