root
|
@--reviews
|
|
@--postABC // postId
|
|
@--reviewXYZ // ***I want everything under this reviewUID
| |
| |--buyerUID: "01010"
| |--text: "fast shipping!"
| |
| @--responseTUV // responseUID
| |
| |--sellerUID: "212555"
| |--text: "we aim to please"
|
@--reviewEFG // this review is from a totally different user I don't want anything from here
我的数据库如上所述进行设置。有时我只有买家uid,因此我使用.queryOrdered(byChild: "buyerUID").queryEqual(toValue: "01010")
提取数据,然后可以从该特定帖子及其评论中找到买家。
找到他们后,如何获取与该节点(reviewXYZ)下的买方关联的所有其他k / v?
let reviewsRef = Database.database().reference()?
.child("reviews")
.child("postABC")
.queryOrdered(byChild: "buyerUID")
.queryEqual(toValue: "01010")
reviewsRef?.observeSingleEvent(of: .value, with: { [weak self](snapshot) in
print(snapshot.key) // prints postABC
print(snapshot.value) // prints a dictionary with reviewXYZ as the key and everything underneath it as a value
guard let dict = snapshot.value as? [String: Any] else { return }
})
答案 0 :(得分:1)
DataSnapshot
class具有内置方法来循环其子级。
reviewsRef?.observeSingleEvent(of: .value, with: { [weak self](snapshot) in
for child in snapshot.children {
let snap = child as! DataSnapshot;
print(snap.key)
print(snap.value)
print(snap.childSnapshot(forPath: "buyerUID").value)
}
})
另请参见其他search results for looping over Firebase child nodes in Swift。