搜索子值 Firebase/Swift [英] Searching through child values Firebase / Swift
问题描述
我的数据库有这样排序的值:
My database has values sorted like this :
Users
UID
Username
Email
我想要实现一个朋友添加系统,您可以在其中搜索用户名或电子邮件,然后添加此人.
I'm wanting to implement a friend adding system where you search for either a username or email and it lets you add the person.
我可以使用
REF_USERS.queryOrdered(byChild: "displayname").queryEqual(toValue: input).observeSingleEvent(of: .value) { (snapshot: FIRDataSnapshot) in {
print(snapshot.value)
}
这样我就得到了用户的整个字典,但是我在获取 UID 时遇到了问题.
With that I get the user's entire dictionary, but I'm having an issue grabbing the UID.
snapshot.key 给我用户".
snapshot.key gives me "Users".
在使用用户名/电子邮件找到用户的字典后,如何从字典中获取 UID 值?
How can I grab the UID value out of the dictionary after finding the user's dictionary with either their username/email?
推荐答案
当您对 Firebase 数据库执行查询时,可能会有多个结果.所以快照包含这些结果的列表.即使只有一个结果,快照也会包含一个结果列表.
When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.
let query = REF_USERS.queryOrdered(byChild: "displayname").queryEqual(toValue: input)
query.observeSingleEvent(of: .value) { (snapshot: FIRDataSnapshot) in {
for child in snapshot.children {
print(child.key)
}
}
另见:
- Firebase snapshot.key 没有返回实际密钥?一个>
- queryOrderBy 中的 Firebase 访问键
- queryOrderBy 中的 Firebase 访问键
- Firebase 按顺序获取数据
这篇关于搜索子值 Firebase/Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!