简体   繁体   English

如何从Firebase中的孩子中删除特定孩子

[英]How to delete a specific child from a child in 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. 此删除将仅由列表所有者进行。

So a scenario will be: 因此,一种情况是:

  • User A creates a List with pushID = 1. 用户A创建一个pushID = 1的列表。
  • This list is added in the following firebase ref: /userList/$userAID/$pushID. 此列表添加在以下Firebase参考中:/ userList / $ userAID / $ pushID。
  • User A shares list with User B and User C. 用户A与用户B和用户C共享列表。
  • This list is added in the following firebase ref: /userList/$userBID/$pushID and /userList/$userCID/$pushID. 此列表添加在以下Firebase参考中:/ userList / $ userBID / $ pushID和/ userList / $ userCID / $ pushID。
  • User A deletes list with pushID = 1. 用户A删除pushID = 1的列表。

So in my 所以在我的

So i have this schema: 所以我有这个架构:

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

In high level this will be: 在较高级别上,这将是:

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

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

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直接转到pushID,并找到所有与我要删除的列表的UID相等的对象,然后再执行此操作。

There is no way to do a server-side delete of multiple items based on a condition with the Firebase Database. Firebase数据库无法根据条件在服务器端删除多个项目。 You must first retrieve the items (or just their IDs) matching the condition and then delete those. 您必须首先检索符合条件的项目(或仅获取其ID),然后将其删除。

However, you can delete the list itself and all references to it in one go by using a multi-path update. 但是,您可以使用多路径更新一次性删除列表本身及其所有引用。

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. 我还建议您保留与特定列表共享的所有UID的列表,这样就不必遍历所有用户。 Keeping many-to-many relations in both directions is quite common in Firebase and other NoSQL databases. 在Firebase和其他NoSQL数据库中,在两个方向上保持多对多关系非常普遍。

For more see: 有关更多信息,请参见:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM