【问题标题】:swift firestore check if documents existsswift firestore检查文件是否存在
【发布时间】:2018-06-21 18:58:57
【问题描述】:

使用 swift 和 firestore 我想检查“已使用的用户名”集合以查看用户名是否已被使用以及是否已提醒用户已使用该用户名,否则如果它仍然可用我想创建文件。

下面概述了我想要做的事情的要点,我可以毫无问题地保存数据,尽管它检查它的文档是否存在然后采取我无法弄清楚的行动

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")
                }
            }
    }
}

【问题讨论】:

    标签: swift google-cloud-firestore


    【解决方案1】:

    以更简洁的方式:

    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")
          }
    }
    

    【讨论】:

    • 记住documentSnapshot是可选值,所以你不能写document.exists,因为这个变量可以为null。你可以这样做: if let documentSnapshot = documentSnapshot, documentSnapshot.exists { }
    【解决方案2】:
    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")
    
    
    
                    }
                }
            }
        }
    }
    

    【讨论】:

    • 请编辑您的答案以添加更多详细信息。仅仅发布代码并不能解释 如何 它是如何工作的,或者为什么它是您问题的正确答案。另外,请删除双换行符,以便您的代码在屏幕上变小。
    【解决方案3】:

    尝试以下方法:

    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.")
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-01
      • 2021-07-22
      • 2020-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      相关资源
      最近更新 更多