如何从Firebase中的孩子中删除特定孩子 [英] How to delete a specific child from a child in Firebase

查看:57
本文介绍了如何从Firebase中的孩子中删除特定孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户可以在其中创建列表的应用程序.

I have an application where a user can create lists.

该用户可以与其他用户共享列表.我已经设法创建共享部分,而我遇到的问题是删除部分.我希望当用户删除共享的列表时,该列表也从其他用户中删除.

The user can share a list with other users. I already manage to create the sharing part, the one I'm having issues is with the delete part. I want that when a user deletes a list that is shared , this list is also deleted from the other users.

此删除操作仅由列表所有者完成.

This delete will be made only by list owner.

因此,场景将是:

  • 用户A创建一个pushID = 1的列表.
  • 此列表添加到以下Firebase参考中:/userList/$ userAID/$ pushID.
  • 用户A与用户B和用户C的共享列表.
  • 此列表添加到以下Firebase参考中:/userList/$ userBID/$ pushID和/userList/$ userCID/$ pushID.
  • 用户A删除pushID = 1的列表.

所以在我的

所以我有这个模式:

userList: {
   2xYnKcZFEdPYWfUJ3E63yQEDShe2: {
     -Kt7lXiY0Yt-oDcV38L5
   }
   KtQHkXMSwKSByZ1rmTRwjDmSYnE3: {
     -Kt7lXiY0Yt-oDcV38L5: {}
     -Kt9XP91hjwcwgcBSgbc: {}
   }
   XHpMVoRqcCdzwTP70L29Lza1ibD3: {
     -Kt7lXiY0Yt-oDcV38L5: {}
   }
}

总的来说,这将是:

userList: {
       userID: (A) {
         -listID : (1) {}
       }
       userID: (B) {
         -listID: (1) {}
         -listID: (2) {}
       }
       userID: (C) {
         -listID: (1) {}
         -listID: (3) {}
         -listID: (4) {}
       }
    }

我当前要执行的操作如下:

The current code I have to do this is the following:

const ref = firebase.database().ref('userList');
    ref.once('value')
      .then((snapshot) => {
        snapshot.forEach((childSnapshot) => {
          ref.child(childSnapshot.key)
            .once('value')
            .then((snapshot2) => {
              snapshot2.forEach((childSnapshot2) => {
                if (childSnapshot2.key === uid) {
                  ref.child(childSnapshot2.key)
                    .remove();
                }
              });
            })
            .catch(() => {
              console.log('error2');
            });
        });
      })
      .catch((error) => {
        console.log(error);
      });

我在这段代码中所做的是,首先通过获取要设法跳转到userID子级的键来获取userList内的所有列表.在该节点内,我再次跳入pushID内,在该处进行验证,以检查当前键是否等于要删除的列表的UID,如果是,则执行remove().

What I'm doing in this code is, first fetching ALL the list inside userList, by getting the key I manage to jump to the userID child. Inside this node once again I manage to jump inside the pushID where I make a validation of checking if current key is equal to the UID of the list i want to delete, if so I do a remove().

我觉得必须有一种更好的方法告诉Firebase直接进入pushID,并找到所有与我要删除的列表的UID相等的对象,然后执行此操作.

I feel there must be a better way of telling Firebase to go directly to pushID and find all of those that are equal to the UID of the list I want to delete and then do it.

推荐答案

无法基于Firebase数据库的条件在服务器端删除多个项目.您必须首先检索符合条件的项目(或仅获取其ID),然后将其删除.

There is no way to do a server-side delete of multiple items based on a condition with the Firebase Database. You must first retrieve the items (or just their IDs) matching the condition and then delete those.

但是,您可以使用多路径更新一次性删除列表本身及其所有引用.

However, you can delete the list itself and all references to it in one go by using a multi-path update.

我还建议保留与特定列表共享的所有UID的列表,这样您就不必遍历所有用户.在Firebase和其他NoSQL数据库中,双向保持多对多的关系非常普遍.

I'd also recommend keeping a list of all the UIDs you've shared a specific list with, so that you don't have to loop over all users. Keeping many-to-many relations in both directions is quite common in Firebase and other NoSQL databases.

有关更多信息,请参见:

For more see:

  • the blog post introducing multi-path updates
  • the blog post describing client-side fan-out using multi-path updates
  • my answer on strategies for updating denormalized data
  • my answer on modeling many-to-many relationships

这篇关于如何从Firebase中的孩子中删除特定孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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