let db = Firestore.firestore()
let currentUser = Auth.auth().currentUser
guard currentUser != nil else {return}
let docRef = db.collection("UserProfiles").document("sWD6Sr11yimSFrpZ9B0m")
docRef.getDocument { document, error in
guard error == nil else {return}
guard let name = document?.get("name") as? String else {return}
guard let lastname = document?.get("lastname") as? String else {return}
self.nameLabel.text = name " " lastname
}
Manual display of current user's documentID = "sWD6Sr11yimSFrpZ9B0m"
How can I get the document ID of the current user automatically?
CodePudding user response:
guard let userId = Auth.auth().currentUser?.uid else {return}
will return current user id who is logged in
you can get all document ids of a collection but not current! firebase doesn't know what is current user document or id. You just get current user uid and save document with uid and get it with uid
db.collection("Seeker").getDocuments { querySnapshot, error in
guard let snapshot = querySnapshot, error == nil else {return}
let documentIDs = snapshot.documents.map({$0.documentID})
print(documentIDs)
}
this is how you get all documents ids ...
if you want to keep track document id
self.db.collection("Seeker").document(userId).setData([
"email": email,
"role" : userType.title
])
so you can get Seeker with his user id as document id