user9043410
user9043410

Reputation:

swift firestore check if documents exists

using swift and firestore I want to check the "Taken User Names" collection to see if a username has been taken and if it has alert the user it taken otherwise if it's still available I want to create the file.

The gist of what I want to do is outlined below, I can save the data no problem though its the checking to see if its document exists then taking action that I cannot figure out

func nextButtonPressed(){

     let db = Firestore.firestore()

    if usernameTextField.text != ""{
        guard let username = usernameTextField.text else { return }
        let docRef = db.collection("Taken User Names").document(username)
        // check if username exists{
        //if exists alert user "sorry user name taken
    } else {
        // if user name doesn't exist 
        db.collection("Taken User Names").document("trinidad")
                .setData(["Taken User Name" : (username)]) {
            (error: Error?) in
                if let error = error {
                   print("\(error.localizedDescription)")
                } else {
                   print("document was succesfully created and written")
                }
            }
    }
}

Upvotes: 9

Views: 10417

Answers (3)

B. Chandresh
B. Chandresh

Reputation: 125

try the following:

let db = Firestore.firestore()
guard let username = userNameTextField.text else { return }

let docRef = db.collection("users").whereField("username", isEqualTo: username).limit(to: 1)
docRef.getDocuments { (querysnapshot, error) in
    if error != nil {
        print("Document Error: ", error!)
    } else {
        if let doc = querysnapshot?.documents, !doc.isEmpty {
            print("Document is present.")
        }
    }
}

Upvotes: 3

Rawand Ahmed Shaswar
Rawand Ahmed Shaswar

Reputation: 2561

In a cleaner way:

let docRef = db.collection("collection").document("doc")
docRef.getDocument { (document, error) in
       if document.exists {
         print("Document data: \(document.data())")
      } else {
         print("Document does not exist")
      }
}

Upvotes: 13

user9043410
user9043410

Reputation:

func nextButtonPressed(){

   let db = Firestore.firestore()

   nextButton.isEnabled = false

    if usernameTextField.text != ""{

        guard let username = usernameTextField.text else { return }

        guard let uid = Auth.auth().currentUser?.uid else { return }

        let docRef = db.collection("Taken User Names").document(username)

        docRef.getDocument { (document, error) in
            if let document = document {


                if document.exists{
                    print("Document data: \(document.data())")

                    self.alertTheUser(title: "Username Taken", message: "please choose again")

                      self.nextButton.isEnabled = true

                } else {

                print("Document does not exist")



                }
            }
        }
    }
}

Upvotes: 6

Related Questions