0

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.enter image description here

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
                    
                }
            }
    }
}
Shy Attoun
  • 21
  • 3
  • The [documentation](https://firebase.google.com/docs/firestore/query-data/get-data) has good examples look at`whereField` – lorem ipsum May 25 '22 at 16:16
  • thanks for your comment, i tried that whereField method, it didn't work out for me though. – Shy Attoun May 25 '22 at 19:10
  • Please share what you did with whereField so we would know what's wrong. More details on how to use it: https://firebase.google.com/docs/firestore/query-data/queries#swift – Asteroid May 25 '22 at 19:23
  • 2
    Does this answer your question? [How to access a specific field from Cloud FireStore Firebase in Swift](https://stackoverflow.com/questions/48312485/how-to-access-a-specific-field-from-cloud-firestore-firebase-in-swift) – Rogelio Monter May 25 '22 at 22:08
  • thanks for your help guys, i found a solution and updated it on my original question. – Shy Attoun May 26 '22 at 09:17
  • @ShyAttoun Good job on finding the solution to your own question. Could you please post your answer as a formal answer to help other users that have a similar problem? – Rogelio Monter May 26 '22 at 22:47

0 Answers0