【问题标题】:get the value of firebase key and value without knowing the parent在不知道父级的情况下获取firebase键和值的值
【发布时间】:2017-04-12 13:48:28
【问题描述】:

我设置了以下数据库:

-Users
-Groups
   -Groupname
      -Cost
      -Creator
      -Members
         -uid1: points
         -uid2: points

仅当当前用户 uid 位于该特定组名的成员区域中时,我才尝试查询以获取组名和成本。问题是我不知道组名,因为它们都将由用户选择,所以它不是静态的。 firebase 数据库是否有任何类型的 getParent 函数?

【问题讨论】:

    标签: swift firebase firebase-realtime-database


    【解决方案1】:

    如果您已经为 Members 节点创建了 FIRDataSnapshot 对象,则可以使用 ref(和 FIRDatabaseReference.parent)属性向上遍历层次结构。例如:

    let members: FIRDataSnapshot = ...
    let groupName: FIRDatabaseReference = members.ref.parent!
    // Inspect the groupName ref...
    

    但是,正如我之前所说,您需要一个方便的FIRDataSnapshot 来完成这项工作:-)

    【讨论】:

    • 我一直在使用 FIRDataSnapshot,但我遇到的问题是我无法访问成员,因为我不知道父级(组名)。有没有办法在整个数据库中搜索 (user.uid) 的任何实例来解决这个问题?在不知道到达 (user.uid) 的整个路径的情况下,我找不到这样做的方法
    • @dgelinas21 我认为这根本不可能,抱歉。您可能需要非规范化您的数据,以便您可以有效地编写查询。这个Firebase guide 可能会帮助你这样做。祝你好运;-)
    • 您必须使用子节点查询所有Groups 循环才能进入Members 节点,抱歉我不知道如何快速开发,但这是唯一的方法
    • @PauloMattos 好的,我将不得不修改我的代码以在加入时将信息添加给用户。只是想确保在这样做之前别无选择。
    • @dgelinas21如果您喜欢冒险,您也可以考虑使用新发布的Cloud Functions 功能。如果您针对多个 client 平台(并且可以容忍一些数据库更新延迟),可能会有所回报。
    【解决方案2】:

    您必须遵守安全规则:-

    安全规则

    {
    "rules": {
        "Groups" : {
    
         "$Group_name" :{
    
            // Will only allow the user's who are members to this group access
            // this group node to read; you can modify your write rules similarly...
    
           ".read" : "root.child('Groups').child($Group_name).child('MemberID').child(auth.uid).exists()",
          ".write" : "auth != null"
    
           }
        },
    
      "Users" : {
    
        "$uid" : {
    
             ".read" : "auth.uid == $uid",
             ".write" : "auth.uid == $uid"
    
          }
        }
      }
    }
    

    安全规则充当内部过滤器;因此,您不必担心父节点不允许用户进入该特定 $Group_name 节点,除非它是该组的成员。

    【讨论】:

    • 如果它解决了您的问题,请接受并投票作为答案,Happy Coding :)
    猜你喜欢
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    相关资源
    最近更新 更多