1

How can I get the value from firstName from the inside:

func saveImage(name: String, postURL:URL, completion: @escaping ((_ url: URL?) -> ())){

    //Get sspecific document from current user
    let docRef = Firestore.firestore().collection("users").whereField("uid", isEqualTo: Auth.auth().currentUser?.uid ?? "")

    var firstName = ""

    // Get data
    docRef.getDocuments { (querySnapshot, err) in

        var firstName = ""

        if let err = err {
            print("ERROR: ")
            print(err.localizedDescription)
            return
        } else if querySnapshot!.documents.count != 1 {
            print("More than one documents or none")
        } else {
            let document = querySnapshot!.documents.first
            let dataDescription = document?.data()
            firstName = dataDescription?["firstname"] as! String



        }

    } 


//         This uploads the data

    let dict = ["title": postDescriptionTitle.text!,
                "description": postDescription.text!,
                "Address": addressField.text!,
                "Zipcode": zipcodeField.text!,
                "timestamp": [".sv":"timestamp"],
                "Author":firstName,
                "postUrl": postURL.absoluteString]
        as [String: Any]
    self.ref.child("post").childByAutoId().setValue(dict)

}

It looks like it's out of scope, how can I store it or access it without storing it in another variable?

As you can see, I'm trying to upload the variable firstName to the database. So in this part: "Author":firstName, I should be getting the value so I can give it to Author

2
  • Why does this function not use its completion block? Why does the completion block provide a url parameter that you don't use? Why not update (and use) the completion block to return the first name value you want? Or why not move the "upload" code inside the block that has the first name? Commented Nov 6, 2019 at 23:14
  • I'd like to do it this way, so I can reuse it later Commented Nov 6, 2019 at 23:15

1 Answer 1

1

Just move the "upload data" part inside the completion block like this:

  func saveImage(name: String, postURL:URL, completion: @escaping ((_ url: URL?) -> ())) {
        //Get sspecific document from current user
        let docRef = Firestore.firestore().collection("users").whereField("uid", isEqualTo: Auth.auth().currentUser?.uid ?? "")

        // Get data
        docRef.getDocuments { (querySnapshot, err) in

            if let err = err {
                print("ERROR: ")
                print(err.localizedDescription)
                return
            } else if querySnapshot!.documents.count != 1 {
                print("More than one documents or none")
            } else {
                let document = querySnapshot!.documents.first
                let dataDescription = document?.data()
                let firstName = dataDescription?["firstname"] as! String
                //         This uploads the data
                let dict = ["title": self.postDescriptionTitle.text!,
                            "description": self.postDescription.text!,
                            "Address": self.addressField.text!,
                            "Zipcode": self.zipcodeField.text!,
                            "timestamp": [".sv":"timestamp"],
                            "Author": firstName,
                            "postUrl": postURL.absoluteString] as [String: Any]
                self.ref.child("post").childByAutoId().setValue(dict)
            }
        }
    }

Also for what are you using the completion argument in your saveImage function?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.