Борьба с базами c Swift logi c (Работа с константами, переменными и циклами) - PullRequest
0 голосов
/ 28 мая 2020

Я хочу использовать 2 строки, которые я получаю с этими двумя «циклами» из firebase, и использовать их в другом «l oop», чтобы загрузить их с кучей другой информации.

Моя проблема в , что я почему-то не могу получить загруженные мной значения fullname и pfp в загрузку в firebase.

Есть идеи, как решить эту проблему?

func sendToFire(){
    let combined = "\(userID)" + "\(number)"
    let docRef = db.collection("posts").document(combined)
    let description = self.textPost.text
    let nameRef = db.collection("users").document(userID)
    var fullname = ""
    var pfp = ""

    if fireImage == nil {

       nameRef.getDocument { (document, error) in
           if let document = document{
               fullname = document.get("fullname") as! String
           }else{
               print("Coulnt get fullname")
           }
       }

       nameRef.getDocument { (document, error) in
           if let document = document{
               pfp = document.get("profileimage") as! String
           }else{
               print("Couldn't get profileimage")
           }
       }

       docRef.getDocument { (document, error) in
           if let document = document, document.exists {
               print("Post ID already taken")
           } else {
               print("Post Document gets created")
               self.db.collection("posts").document(combined).setData([
                   "description": description!,
                   "likes": self.likes,
                   "postType": 0,
                   "profileImage": pfp,
                   "time": self.date,
                   "uid": self.userID,
                   "username": fullname
               ]) { err in
                   if let err = err {
                       print("Error writing document: \(err)")
                   } else {
                       print("Post Document successfully written!")
                   }
               }
           }
       }
    }
}

1 Ответ

0 голосов
/ 28 мая 2020

Добавьте document.exists в , если разрешите

        nameRef.getDocument { (document, error) in
            if let document = document, document.exists{
                fullname = document.get("fullname") as! String
            }else{
                print("Coulnt get fullname")
            }
        }

        nameRef.getDocument { (document, error) in
            if let document = document, document.exists{
                pfp = document.get("profileimage") as! String
            }else{
                print("Couldn't get profileimage")
            }
        } 

Проверьте фактические имена ключей в ответе fullname и profileimage.

...