Firestore数据库和存储的问题

问题描述 投票:0回答:1

我试图在存储中保存一张图片,获取该图片的URL,然后在数据库的一个字段中停止URL。问题是图片被保存了,但数据库中的文档信息却没有被保存,我试过延迟存储部分,但没有任何效果。我不确定是否是URL的问题,似乎如果图像已经被保存,那么重新保存文档就可以了。下面是我的功能:任何帮助或指导将是感激的(我是一个新的编码)。

 @IBAction func submitButtonTapped(_ sender: Any) {
    let db = Firestore.firestore()
    if let userName = Auth.auth().currentUser?.email {
        print(userName)

    func validateFields() -> String? {

                //Check that all fields are filled in
                if mustangNameTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" || blmTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" || roundedUpTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" || bioTextView.text?.trimmingCharacters(in: .whitespacesAndNewlines) == ""  {
                    return "Please fill in all fields"
                }
            return nil
        }

        let error = validateFields()

        if error != nil {
            // There is something wrong with the fields, show the error message
              let e = error
              print("Error in data, \(String(describing: e))")
              } else {

               //Create cleaned version of the data
               var mustangName = mustangNameTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
               let blm = blmTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
               let roundedUp = roundedUpTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
               let bio2 = bioTextView.text!.trimmingCharacters(in: .whitespacesAndNewlines)

            let storageRef = Storage.storage().reference().child("Profiles/\(mustangName)")
            if let uploadData = self.profileImageView.image!.pngData() {
            let metaDataForImage = StorageMetadata()
                metaDataForImage.contentType = "image/png"
                storageRef.putData(uploadData, metadata: metaDataForImage, completion: { (metadata, error) in
                        if error != nil {
                        print(error!)
                        return
                        }
                        print(metaDataForImage)
                        } )
                storageRef.downloadURL { (url, error) in
                    guard let downloadURL = url?.absoluteString else {
                           return
                          }
                    if url?.absoluteString != "" {
                        return
                    } else {
                    }
                            print(url!)
                            print("got url")
                    db.collection("Profile").document(mustangName).setData(["mustangName": mustangName,"blm": blm, "roundedUp": roundedUp, "bio2": bio2, "userName": userName, "profileImageURL": url?.absoluteString as Any]) {(error) in
                        if let e = error {
                        print("Error saving user data to firestore, \(e)")
                        } else {
                        print("Successfully saved data for: \(userName)")
                        }
                    }
                    func showError(_ message: String){
                    self.errorLabel.text = message
                    self.errorLabel.alpha = 1
                    }
                }
            }
        }
    }
    //close profile window and go to home screen
       let homeViewController = self.storyboard?.instantiateViewController(identifier: Constants.Storyboard.homeViewController) as? HomeViewController
          self.view.window?.rootViewController = homeViewController
          self.view.window?.makeKeyAndVisible()

}
swift google-cloud-firestore firebase-storage
1个回答
0
投票

我强烈建议总是用代码格式化器来格式化你的代码,因为这可以使你更容易看到问题。

在这种情况下,你需要移动获得下载URL的代码,并将其写入Firestore。变成 文件上传完成后运行的完成处理程序。

let storageRef = Storage.storage().reference().child("Profiles/\(mustangName)")
if let uploadData = self.profileImageView.image!.pngData() {
    let metaDataForImage = StorageMetadata()
    metaDataForImage.contentType = "image/png"
    storageRef.putData(uploadData, metadata: metaDataForImage, completion: { (metadata, error) in
        if error != nil {
            print(error!)
            return
        }
        print(metaDataForImage)

        storageRef.downloadURL { (url, error) in
            guard let downloadURL = url?.absoluteString else {
                return
            }
            if url?.absoluteString != "" {
                return
            }
            else {
            }
            print(url!)
            print("got url")
            db.collection("Profile").document(mustangName).setData(["mustangName": mustangName,"blm": blm, "roundedUp": roundedUp, "bio2": bio2, "userName": userName, "profileImageURL": url?.absoluteString as Any]) { (error) in
                if let e = error {
                    print("Error saving user data to firestore, \(e)")
                }
                else {
                    print("Successfully saved data for: \(userName)")
                }
            }
            func showError(_ message: String){
                self.errorLabel.text = message
                self.errorLabel.alpha = 1
            }
        }
    })
}
© www.soinside.com 2019 - 2024. All rights reserved.