简体   繁体   English

Firebase如果我不认识两个孩子的ID不明,请访问他们的孩子

[英]Firebase Access a children if I don't know two childs who have unknown ids

How can I access the data of the children memberId or name and photoURL of the child "members"? 如何访问子成员memberId的数据或子“成员”的名称和photoURL? You can see the structure of my database in images. 您可以在图像中看到我的数据库的结构。 在此处输入图片说明

在此处输入图片说明

I tried to use queryOrdered and queryEqual but I just can use it one time 我试图使用queryOrdered和queryEqual但我只能使用一次

I tried like that because I know the room.key who is the "key" on the database. 我之所以这样尝试是因为我知道room.key是数据库上的“键”。

let refParticipants = refDatabase.child("markers").queryOrdered(byChild: "key").queryEqual(toValue: room.key)
    refParticipants.observe(.childAdded, with: { snapshot in
...
}

I use Swift 3.1 我使用Swift 3.1

I update my answer with that screenshot: 我用该屏幕截图更新了答案: 在此处输入图片说明

I think you are asking how to access the child nodes of 我认为您在问如何访问的子节点

/markers/oHQ.../members/9oBKY...

Let's simply the structure for this answer 让我们简单地将此答案的结构

markers
  marker_0
   members
     member_0
       name: "J"
     member)1
       name: "K"

and then the code that will access each member within the members node and print their name 然后将访问成员节点中每个成员并打印其名称的代码

let markersRef = self.ref.child("markers")
let marker0Ref = markersRef.child("marker_0")
let membersRef = marker0Ref.child("members")
membersRef.observeSingleEvent(of: .value, with: { snapshot in
    for child in snapshot.children {
        let snap = child as! DataSnapshot
        let dict = snap.value as! [String: Any]
        let name = dict["name"] as! String
        print(name)
    }
})

and the output will be 和输出将是

J
K

Since you know the parent node (oHQa...), which contains the child node 'members', it doesn't really matter as to what each members key is since you are iterating over them. 由于您知道包含子节点“ members”的父节点(oHQa ...),因此,每个成员键的作用都无关紧要,因为您正在对其进行迭代。

However, if you are wanting to query for certain members or other member data, you may want to consider flattening the database a bit like this 但是,如果要查询某些成员或其他成员数据,则可能需要考虑将数据库变平一​​些

markers
  oHQa...
    //marker data
marker_members
  member_0
     name: "J"
     member_of: "oHQa..."
  member_1
     name: "K"
     member_of: "oHQa..."

With this structure you can query for all the members of any marker or query for all members whose name is "J" etc. 通过这种结构,您可以查询任何标记的所有成员,也可以查询名称为“ J”的所有成员,等等。

As a side note, in the structure in the question you have the member_id as both the key as well as a child which is unnecessary. 附带说明一下,在问题的结构中,您既有member_id作为键又有子级,这是不必要的。 If it's the key then you can always directly access that node without a query. 如果这是关键,那么您始终可以直接访问该节点而无需查询。

暂无
暂无

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

相关问题 如果不知道其密钥,如何在Firebase中更新节点? - How to update a node in Firebase if I don't know its key? 我不知道为什么结果不是我想的 - I don't know why the result is not my have thought 我在 SwiftUI 中找到了两种管理视图的方法,但我不知道有什么区别,也不知道哪种方法更好 - I found two ways to manage views in SwiftUI, but I don't know what the difference is and I don't know which is the better way Apple Demo没有“ performSelector可能会导致泄漏,因为其选择器未知”警告。 有人知道为什么吗? - Apple Demo don't have “performSelector may cause a leak because its selector is unknown” Warning. Anyone know why? 我是否可以为具有不同捆绑ID的两个iOS应用使用相同的Firebase GoogleService-Info.plist? - Can I use the same Firebase GoogleService-Info.plist for two iOS apps that have different bundle ids? 应用程序推送证书已过期,我无权访问证书 - application push certificat was expired, i don't have access to certificat 我有一个有效的滑动检测器脚本,但我不知道如何将它连接到我的角色控制器 - I have a working swipe detector script, but I don't know how to connect it to my character controller 不知道该怎么做才能添加Game Center排行榜 - don't know what I have to do to add Game Center leaderboard 当弹丸击中两个“怪物”时,didBeginContact方法崩溃。 我知道为什么,但我不知道如何避免 - When projectile hits two “monsters” the didBeginContact method crashes. I know why but i don't know how to avoid it 如何在我不知道的对象上防止EXC_BAD_ACCESS在iOS 9中发布? - How to prevent EXC_BAD_ACCESS on an object I don't know is released in iOS 9?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM