快速消防站检查文件是否存在 [英] swift firestore check if documents exists

查看:34
本文介绍了快速消防站检查文件是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用swift和firestore,我想检查已接受用户名"集合,以查看是否已使用用户名,是否已警告用户已使用用户名,否则如果仍然可用,我想创建文件.

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

推荐答案

以一种更简洁的方式:

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

这篇关于快速消防站检查文件是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆