从Firestore动态删除数据 [英] Delete data from Firestore dynamically

查看:33
本文介绍了从Firestore动态删除数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与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.

推荐答案

如何从tableGuests中删除字段?

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. 使用 const data = firestore().collection('userTables').doc(ID).get();
  2. 来获取表数据
  3. 使用该数据获取数组的当前状态 const array = data.get('tableGuests');
  4. 使用不包含要删除的项目的新数组更新文档(在本例中为最后一个) firestore().collection('userTables').doc(ID).update({tableGuests:array.slice(0,array.length-1)});

这篇关于从Firestore动态删除数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆