title: Changing from Firebase to Firestore caused Fatal error: Unexpectedly found nil ... in a query
body: <p>I am trying to retrieve a user's data from firestore, after migrating my data there from firebase. The code below was working fine with firebase, and retrieved the user's data.
  <a href="https://i.stack.imgur.com/OPbHB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OPbHB.png" alt="Firebase query"></a></p>
  
  <p>However, after changing the query to firestore query, I got this error. 
  <a href="https://i.stack.imgur.com/qaKlY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qaKlY.png" alt="FireStore query"></a></p>
  
  <p>Print statement here "document.data()" contains the data, But I got this error. I don't know where this error is coming from. </p>
  
  <p>When I compare document.data() with nil, I got "Document data: contains nil".
   <a href="https://i.stack.imgur.com/uvxCI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uvxCI.png" alt="Document data: contains nil"></a></p>
  
  <p>I don't know how I suppoused to get the data.  </p>
  
  <p>here is the code where I get the error,</p>
  
  <pre><code>    static func getUser(uid: String, setUserDefaults: @escaping (NormalUser) -&gt; Void){
     DataService.ds.REF_USERS_NORMAL.document(uid).getDocument { (document, error) in
          if error != nil{
              print("\(String(describing: error?.localizedDescription))")
          }else{
                  if document != nil{
                      let data = document?.data() as! [String: String]
                     print("Document data: \(String(describing: document?.data() as! [String: String]))")
                      let user = NormalUser(userData: (data as Dictionary&lt;String, AnyObject&gt;))
                      setUserDefaults(user)
                  }else{
                      print("Document data: contains nil")
                  }
          }
      }
  }
  </code></pre>
  
  <p>Here is how I defined the variables,</p>
  
  <pre><code> import Foundation
   import Firebase
  
  class NormalUser: User {
  
  private var _email: String?
  private var _city: String?
  private var _country: String?
  private var _name: String?
  private var _phone: String?
  private var _profileImgUrl: String?
  
  
  var email: String {
      return _email!
  }
  var city: String {
      return _city!
  }
  var country: String {
      return _country!
  }
  var name: String {
      return _name!
  }
  var phone: String {
      return _phone!
  }
  var profileImgUrl: String {
      set{
          self.profileImgUrl = _profileImgUrl!
      }
      get{
          if let pI = _profileImgUrl{
              return pI
          }
          return ""
      }
  }
  
  init(userData: Dictionary&lt;String, AnyObject&gt;) {
  
      super.init(userId: userData["uid"] as! String, user: userData)
  
      if let email = userData["email"] as? String {
          self._email = email
      }
      if let city = userData["city"] as? String {
          self._city = city
      }
      if let country = userData["country"] as? String {
          self._country = country
      }
      if let name = userData["name"] as? String {
          self._name = name
      }
      if let phone = userData["phone"] as? String {
          self._phone = phone
      }
      if let profileImgUrl = userData["imgUrl"] as? String {
          self._profileImgUrl = profileImgUrl
      }
  }
  
  static func createNormalUser(uid: String, userData: Dictionary&lt;String, String&gt;) {
      // add user to database
      //DataService.ds.REF_USERS_NORMAL.child(uid).setValue(userData)
      DataService.ds.REF_USERS_NORMAL.document(uid).setData(userData) { (err) in
              if let err = err {
                  print("Error writing document: \(err)")
              } else {
                  print("Document successfully written!")
              }
      }
  
      addUserToGroup(uid:uid, group:"normal")
  }
  
  static func updateUserProfile(uid: String, userData: Dictionary&lt;String, String&gt;) {
      //DataService.ds.REF_USERS_NORMAL.child(uid).updateChildValues(userData)
     DataService.ds.REF_USERS_NORMAL.document(uid).updateData(userData)
  }
  
  static func getUser(uid: String, setUserDefaults: @escaping (NormalUser) -&gt; Void){
  
     DataService.ds.REF_USERS_NORMAL.document(uid).getDocument { (document, error) in
          if error != nil{
              print("\(String(describing: error?.localizedDescription))")
          }else{
                  if document != nil{
                      let data = document?.data() as! [String: String]
                     print("Document data: \(String(describing: document?.data() as! [String: String]))")
                      let user = NormalUser(userData: (data as Dictionary&lt;String, AnyObject&gt;))
                      setUserDefaults(user)
                  }else{
                      print("Document data: contains nil")
                  }
          }
      }
  }
  </code></pre>
  
  <p>}</p>