简体   繁体   English

Firebase子孩子已删除

[英]Firebase sub-child removed

Is there any way to get if subchild removed. 如果删除了子孩子,有什么办法吗? db structure is: db结构为:

users:{
  a: {
    friends: {
      b:Jhon,
      c:Ted
  },
  b: { 
    friends: {
      a: Tom
    }
  },
  c: {
    friends:{
      a: Tom
    }
  }
}

I want get event while anybody removes any child from friends. 我想得到事件,而任何人都从朋友中删除任何孩子。 Attaching event to every user is not a good way. 将事件附加到每个用户不是一个好方法。
I am trying in node.js admin 我正在尝试在node.js管理员中

Here is another way, you can create a special node for "friend removal request", so the client only writes to this node, then the server handles the deletion. 这是另一种方法,您可以为“删除朋友的请求”创建一个特殊的节点,因此客户端仅写入该节点,然后服务器处理删除。

First, look at this database structure 首先,看一下这个数据库结构

{
    users: {
        a: {
            friends: {
                b:Jhon,
                c:Ted
            },
        }
        b: { 
            friends: {
                a: Tom
            }
        },
        c: {
            friends:{
                a: Tom
            }
        }
    }
    friend_removal_request : {
        <push_id>: {
            first_user: a,
            second_user: b
        }
    }
}
  • The server attach child_added listener to friend_removal_request 服务器将child_added侦听器附加到friend_removal_request
  • If a user wants to delete a friend of their, their client writes a new entry (by using DatabaseReference.push() method) to friend_removal_request . 如果用户想要删除其朋友,则其客户端(通过使用DatabaseReference.push()方法)将新条目写入friend_removal_request first_user is the user's id, second_user is their friend's id first_user是用户的ID, second_user是他们的朋友的ID
  • Then the listener at the server will be fired, the server now can proceed to delete their friendship by setting a null value (or call remove() ) to /users/$first_user/friends/$second_user and /users/$second_user_user/friends/$first_user 然后将触发服务器上的侦听器,服务器现在可以通过将null值(或调用remove() )设置为/users/$first_user/friends/$second_user/users/$second_user_user/friends/$first_user来删除其友谊/users/$second_user_user/friends/$first_user
  • After the friendship is deleted, the server deletes that request snapshot.ref.remove() 删除友谊后,服务器将删除该请求snapshot.ref.remove()

Now, the listener is only attached to a node. 现在,侦听器仅附加到一个节点。

This is my idea, let me know if you have another concern with this method. 这是我的主意,如果您对此方法还有其他疑问,请告诉我。 Hope this helps :) 希望这可以帮助 :)

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

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