1

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")
        }
    }
Doug Stevenson
  • 268,359
  • 30
  • 341
  • 380
Alwatkin
  • 11
  • 1

1 Answers1

0

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.

fake girlfriends
  • 10,377
  • 2
  • 38
  • 42