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
  • 297,357
  • 32
  • 422
  • 441
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.

trndjc
  • 11,654
  • 3
  • 38
  • 51