title: Firebase for SWIFT iOS: returning nil value in function
body: <p>I am trying to access my firestore database which store the userid, username in the collection called user, I want to return the username of the current user, however when I try to run the below function, it is returning me nil value.... do you know anything that I'm missing? A completion handler?</p>
  <pre><code>func display(userid: String) -&gt; String
  {
      var displayname: String
      let docRef = db.collection(&quot;user&quot;).document(uid)
      docRef.getDocument { (document, error) in
          if let document = document, document.exists {
              displayname = document.get(&quot;username&quot;) as! String
              
          } else {
              print(&quot;Document does not exist&quot;)
          }
      }
      return displayname
  }
  </code></pre>
  <hr />
  <p>Then I tried to change to this but it still doesn't change my variable. It seems like it's trapped inside my function</p>
  <pre><code>var name = &quot;placeholder&quot;
  
  func display(userid: String, completion: @escaping (String) -&gt; Void) {
      let docRef = db.collection(&quot;user&quot;).document(userid)
      docRef.getDocument { (document, error) in
          if let document = document, document.exists {
              if let displayName = document.get(&quot;username&quot;) as? String {
                  self.name = displayName
                  completion(displayName)
              }
          } else {
              print(&quot;Document does not exist&quot;)
          }
      }
  }
  
  func create()
  {
      display(userid: uid){
          [weak self] displayName in
          print(displayName)
          self!.name = displayName
      }
  var ref: DocumentReference? = nil
  ref = db.collection(&quot;Request&quot;).addDocument(data: [&quot;requestername&quot;: &quot;name&quot;]
  
  .....
  }
                                                  
      
  </code></pre>