问题描述
我正在与React&Redux&Firestore,我知道如何删除/更新/添加等.但是当我有2个带有动态字段的子集合时,找不到任何解决方案来删除它.
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.
如果看图片,我有表ID和用户ID,但字段是0、1、2等.
If you look at the picture, I have the table ID and user ID, but the fields are 0, 1, 2 and so on.
如何从tableGuests中删除字段?也许结构不好,可能会更好?
How can I delete a field from tableGuests?Maybe the structure is not good and could be better?
guests>用户ID> userTables>表ID> tableGuests,它是一个数组.
guests > user id > userTables > table id > tableGuests which is an array.
推荐答案
文档中没有明确的方法来说明如何从数组中弹出项目.我会这样做:
There is no clear way in the docs that explains how to pop an item from an array.I would do this:
- 使用
const data = firestore().collection('userTables').doc(ID).get();
来获取表数据 - 使用该数据获取数组的当前状态
const array = data.get('tableGuests');
- 使用不包含要删除的项目的新数组更新文档(在本例中为最后一个)
firestore().collection('userTables').doc(ID).update({tableGuests:array.slice(0,array.length-1)});
这篇关于从Firestore动态删除数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!