搜索儿童价值Firebase / Swift [英] Searching through child values Firebase / Swift

查看:128
本文介绍了搜索儿童价值Firebase / Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库有这样排序的值:

 用户
UID
用户名
电子邮件

我想要实现一个朋友添加系统,您可以搜索用户名或电子邮件,它可以让你添加该人。



我可以通过使用

 < $ {$ b $ print(snapshot.value)$ b中的queryEqual(toValue:input).observeSingleEvent(of:.value){(snapshot:FIRDataSnapshot)code> REF_USERS.queryOrdered(byChild:displayname 



$ p
$ b $ p

我得到了用户的整个字典,但是我遇到了一个问题, UID。



snapshot.key给我用户。

如何获取UID值使用用户名/电子邮件查找用户字典后的字典?

解决方案

当您对Firebase数据库执行查询时,将潜在的多个结果。所以快照包含了这些结果的列表。即使只有一个结果,快照也会包含一个结果列表。

  let query = REF_USERS.queryOrdered(在$ {
中为快照.children中的子元素{
print()}中的queryEqual(toValue:input)
query.observeSingleEvent(of:.value){(snapshot:FIRDataSnapshot) (child.key)
}
}

另见:




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.

I'm able to locate users by using

 REF_USERS.queryOrdered(byChild: "displayname").queryEqual(toValue: input).observeSingleEvent(of: .value) { (snapshot: FIRDataSnapshot) in {
 print(snapshot.value)
}

With that I get the user's entire dictionary, but I'm having an issue grabbing the UID.

snapshot.key gives me "Users".

How can I grab the UID value out of the dictionary after finding the user's dictionary with either their username/email?

解决方案

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)
    }
}

Also see:

这篇关于搜索儿童价值Firebase / Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆