as I mentioned in the title, im trying to pull data from a specific field, the idea is the user is typing a certain number in the search bar, if the number (car license number) exist the user can contact the car owner.
what im trying to do is checking this field throughout all users.
my current code :
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchText == "" {
carNumberLbl.text = ""
}else {
getData ()
}
}
func getData (){
let docRef = db.collection("Users").document("2MHOob4kgh8JOu6UXxfb")
docRef.getDocument { (document, error) in
if let document = document, document.exists {
let carNumber = document.get("car_license")
self.carNumberLbl.text = carNumber as! String
print(carNumber)
}else
{
print("Couldn't find data")
}
}
}
Solution!
func getDataSpecificField (){
db.collection("Users").whereField("car_license", isEqualTo: self.searchBarCars.text!)
.getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {
let carNumber = document.get("car_license") as! String
self.carNumberLbl.text = carNumber
}
}
}
}