[英]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 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.