ios - 检查firebase快照是否等于文本字段输入
问题描述
我正在尝试检查文本输入是否等于 firebase 数据库中的值。我得到以下错误
“捕捉(位置)空”
即使我输入了一个我知道在数据库中的值。我真的很感激一些帮助 :-) 下面是我的 JSON 文件。
{
"locations" : {
"115 W Perry Carriage House" : {
"City" : "Savannah",
"State" : "GA",
"img" : " "
},
"115 W Perry Street" : {
"City" : "Savannah",
"State" : "GA",
"img" : " "
},
"117 West Charlton Street" : {
"City" : "Savannah",
"State" : "GA",
"img" : " "
},
"127 Coming Street Unit C" : {
"City" : "Charleston",
"State" : "SC",
"img" : " "
和代码:
let databaseRef = Database.database().reference()
databaseRef.child("locations")
.queryOrdered(byChild: "locations")
.queryStarting(atValue: addressTextField.text)
.observe(DataEventType.value, with:
{
(snapshot) in
print(snapshot)
if snapshot.exists(){
print("Address is in DB")
}else{
print("Address doesn't exist")
}
})
}
解决方案
代码当前在您的问题中使用的查询正在查看嵌套在“位置”下的名为“位置”的子项的值。因此,如果您想象查询查找数据,它会在这里拉第一个孩子,而不是第二个孩子。
{
"locations" : {
"115 W Perry Carriage House" : {
"locations": "115 W Perry Carriage House", // <- here's one!
"City" : "Savannah",
"State" : "GA",
"img" : " "
},
"115 W Perry Street" : { // <- hmm...this one doesn't have "locations"
"City" : "Savannah",
"State" : "GA",
"img" : " "
},
//...
}
由于数据结构不包含任何称为“位置”的子节点,因此没有任何值与您要查找的值相匹配。由于您希望地址与孩子的键匹配,因此我们无需查询即可获取该孩子的数据。我们可以观察到确切的路径,如下所示:
let databaseRef = Database.database().reference()
guard let text = addressTextField.text else { return }
databaseRef.child("locations/\(text)")
.observe(.value, with: { (snapshot) in
print(snapshot)
if snapshot.exists() {
print("Address is in DB")
} else {
print("Address doesn't exist")
}
})
推荐阅读
- swashbuckle - OSS 规范中的服务器元素
- javascript - 如何定期检查状态并与请求同步?
- ssl - 使用 HAProxy 入口控制器时,TLS 握手间歇性失败
- c - 为什么 glibc 中 x86 的读写屏障不使用 __volatile asm?
- javabeans - 服务任务中的 Flowable delegateExpression 不起作用
- microsoft-graph-api - 如何从 OneNote API 中检索按标签过滤的页面?
- r - R furrr:在运行计算之前对每个未来进程的 API 进行身份验证
- c - C:操作符'->'的问题,编译但返回错误。有main.c和.h、.c文件
- visual-studio-2019 - 如何在 EnvDTE.Events.DebuggerEvents 的处理程序中确定已调试的程序/进程?
- azure - 在哪里查看 azure 中的 k8s 版本“过期”(无法升级)的日期?