image of data tree尝试使用搜索栏搜索数据库中的名称。试图打印数据但没有打印,我也没有收到任何错误。我最初收到数据库规则的错误,但我修复了这一点,但仍然没有。 “DATA DOES NOT EXIST
”也没有打印。
这是我的代码:
func searchBar(_ searchBar: UISearchBar, textDidChange searchText:
String) {
if searchBar.text == nil || searchBar.text == "" {
inSearchMode = false
view.endEditing(true)
tableView.reloadData()
} else {
inSearchMode = true
search = searchBar.text
searchDatabase(name: search)
self.tableView.reloadData()
}
}
func searchDatabase(name: String!) {
let dbRef = Database.database().reference().child("users")
dbRef.queryOrdered(byChild: "fullname").queryEqual(toValue: name).observeSingleEvent(of: .value, with: { (snapshot) in
if !snapshot.exists() {
// Name doesnt exist
print("DATA DOES NOT EXIST")
}
if let data = snapshot.value as? [String: [String: String]] {
//it should exist if it reaches here
print(data)
}
})
}
以下是数据库规则:
{
"rules": {
"users": {
".read": true,
".write": "auth != null",
".indexOn" : ["fullname"]
},
"posts": {
".read": true,
".write": "auth != null"
}
}
}
数据库结构是这样的:
"users": {
"uid": {
"fullname": "firstname lastname",
"username": "12345user"
}
}