Would like to identify the auto generated id to reference in different query.
{
docRef = db.collection("users").document()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let document = docRef.documentID.self as? Int ?? {
let dataDescription = document.data().map(Int.init(describing:)) ?? "nil"
let profileName = dataDescription["firstname"] as? String ?? ""
self.nameLabel.text = profileName
print("Document data: \(profileName)")
} {
print("Document does not exist")
}
}
Declare the document reference as a property before you create the document:
let userDocRef = Firestore.firestore().collection("users").document()
Then when you need the document ID in a future query, get it from that property:
let docID = userDocRef.documentID
This may require you to pass userDocRef
forward to the object that performs the query, or exposing the property to that object. Either way, it's all a matter of scope and how you pass it around or access it is up to your preference.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.