0

I have the following code that is querying Firestore to get the name of a user based on their deviceIdentifier.

  func getUserName(deviceUid: String) -> String {
    var name = ""
    let db = Firestore.firestore()
    db.collection("users").document(deviceUid).getDocument { document, error in
      if let document = document, document.exists {
        let dataDescription = document.data()
        print("dataDescription: \(dataDescription)")
        name = dataDescription?["name"] as! String
      } else {
        print("Document does not exist")
      }
    }
    return name
  }

I can see that the data is in Firestore because I get the following in the console logs: dataDescription: Optional(["name": xxx, "deviceUid": xxx-xxx-xxxx-xxxx]) but the method isn't returning the name.

HarryMoy
  • 142
  • 3
  • 20

0 Answers0