为什么我的Firebase存储URL没有上传到Google Cloud Firestore? [英] Why aren't my Firebase Storage URLs uploading to Google Cloud Firestore?

查看:64
本文介绍了为什么我的Firebase存储URL没有上传到Google Cloud Firestore?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图允许用户将个人资料照片上传到Firebase Storage.图片在我的应用注册过程中上传.图像已正确上传到Firebase存储,但是下载URL未上传到Google Cloud Firestore.

I am trying to allow users to upload a profile photo to Firebase Storage. The image gets uploaded during the sign up process of my app. The image is being uploaded to Firebase Storage properly, however, the download URL is not uploading to Google Cloud Firestore.

我已经尝试更改类型为String的变量downloadURL来容纳空的String.这并没有改变结果.

I've already tried to change my variable downloadURL of type String to hold an empty String. This did not change the result.

以下是将个人资料照片上传到Firebase Storage的代码:

Here is the code to upload the profile photo to Firebase Storage:

func uploadProfilePic() -> String {
     guard let imageData = profilePic?.jpegData(compressionQuality: 0.75) else {
         print("Unable to get image data.")
         return ""
     }

     let imageID = UUID().uuidString
     let profilePhotosRef = Storage.storage().reference().child("profilePhotos/\(imageID).jpg")

     let uploadMetadata = StorageMetadata()
     uploadMetadata.contentType = "image/jpeg"

     var downloadURL = String()
     profilePhotosRef.putData(imageData, metadata: uploadMetadata) { (downloadMetadata, err) in
         if let err = err {
             self.showAlertWithOK(title: "An Error Occurred", message: "There was an error uploading your profile photo. Try again later in Hostend settings.")
             print("An error occurred: \(err.localizedDescription)")
         }

         profilePhotosRef.downloadURL { (url, err) in
             if let url = url {
                 downloadURL = url.absoluteString
             }
         }
     }

     return downloadURL
}

以下是用于将个人资料照片下载网址上传到Cloud Firestore的代码:

Here is the code for uploading the profile photo download URL to Cloud Firestore:

db.collection("users").document(uid).setData(["profile_pic_url": uploadProfilePic(), "name": name, "phone_number": phoneNumber, "fcm_token": token, "timestamp": Date()])

uploadProfilePic()方法返回一个String.

我希望将图像的下载URL上传到Firestore的"profile_pic_url"下,但这没有发生.相反,它只是一个空的String,即使该映像已成功存储到Firebase Storage中.

I expected the download URL of the image to be uploaded to Firestore under "profile_pic_url," but that did not happen. Instead, it is just an empty String even though the image had been successfully to Firebase Storage.

推荐答案

您不能对包含闭包的函数使用return语句,因为该函数将在闭包执行之前返回.

You can't use a return statement for a function that contains a closure as the function will return before the closure has executed.

改为将您的函数更改为使用补全处理程序,例如

Instead change your function to use a completion handler such as

func uploadProfilePic(completion: @escaping (String?, Error?) -> ()) {

然后,当您获得下载网址后,请调用处理程序.

Then once you get your download url call the handler.

    profilePhotosRef.downloadURL { (url, err) in
      completion(url, err)
    }

然后您可以使用此功能像这样填充对Cloud Firestore的呼叫

You can then use this function to populate your call to Cloud Firestore like so

   self.uploadProfilePic() { (url, error) in

    guard error....

    if let url = url {
      // do your upload here
    }
}

这篇关于为什么我的Firebase存储URL没有上传到Google Cloud Firestore?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆