Osman Kaya Erdoğan
Osman Kaya Erdoğan

Reputation: 115

How I can get document ID in Firebase?

I need to DocumentID in the photos. How can I get this document or collection ID ? I couldn't find how to implement subtrees pathway. I want to get document or collection id and use in getUserInfo().

I need these ID's

let fireStoreDatabase = Firestore.firestore()


override func viewDidLoad() {
    super.viewDidLoad()
    getUserInfo()

}


@IBAction func createClicked(_ sender: Any) {

    let documentData = ["email":UserSingleton.sharedUserInfo.email,"username":UserSingleton.sharedUserInfo.username,"Housename":UserSingleton.sharedUserInfo.housename] as [String:Any]

    fireStoreDatabase.collection("Phome").document().collection("Users").addDocument(data: documentData) { (error) in
        if error != nil {
            //
        }


    }

}
func getUserInfo(){
fireStoreDatabase.collection("UserInfo").whereField("email", isEqualTo: Auth.auth().currentUser!.email!).getDocuments { (snapshot, error) in
    if error != nil {
        self.makeAlert(title: "error", message: error?.localizedDescription ?? "Error")
    }else{
        if snapshot?.isEmpty == false && snapshot != nil{
            for document in snapshot!.documents{
              if let username =  document.get("username") as? String{
                UserSingleton.sharedUserInfo.email = Auth.auth().currentUser!.email!
                UserSingleton.sharedUserInfo.username = username
            //  UserSingleton.sharedUserInfo.housename = THIS WILL BE HOUSENAME

                }
            }
        }
    }
}

}

this is the class singleton :

class UserSingleton{

static let sharedUserInfo = UserSingleton()

var email = ""
var username = ""
var housename = ""




private init(){

}
}

Upvotes: 0

Views: 45

Answers (1)

MobileMon
MobileMon

Reputation: 8651

you just need to call .documentID on the document object

for document in snapshot!.documents{
       let documentID = document.documentID       
}

Upvotes: 1

Related Questions