首页 > 解决方案 > 如何将配置文件链接上传到 Firebase 数据库

问题描述

当我的“ ProfileImageuUrl ”出现在firebase数据库中时,配置文件图像的链接丢失,这导致我的用户配置文件失火,图像不会显示,因为firebase数据库中没有链接我该如何重组我的代码到这个问题

if let AuthData = AuthDataResult {
                print(AuthData.user.email)
                let dict : Dictionary < String, Any> = [
                    "uid": AuthData.user.uid,
                    "email": AuthData.user.email,
                    "ProfileImageUrl": "",
                    ]
             if let profileImge = self.selectedImage, let imageData = profileImge.jpegData(compressionQuality: 0.1) {
             let StorageRef = Storage.storage()
                    .reference(forURL: "gs://tunnel-vision-458d6.appspot.com/")
                let storageProfileRef = StorageRef.child("profileImage").child(AuthData.user.uid)

                let metadata = StorageMetadata()
                metadata.contentType = "profileImage.jpg"
                storageProfileRef.putData(imageData, metadata: metadata) { (storageMetadata, error) in
                    if error != nil {
                    return
                }

                    storageProfileRef.downloadURL { (url, error) in
                        if let metaImageUrl = url?.absoluteString {
                        print(metaImageUrl)
                    }


                        Database.database().reference().child("users").child(AuthData.user.uid).updateChildValues(dict, withCompletionBlock: {
                            (error,ref) in
                            if error == nil {
                                print("Done")

                            }

标签: iosswiftfirebasefirebase-realtime-databaseuser-profile

解决方案


You are assigning an empty string for ProfileImageUrl in dict:

let dict : Dictionary < String, Any> = [
    "uid": AuthData.user.uid,
    "email": AuthData.user.email,
    "ProfileImageUrl": "",
]

And you never update it afterward. The dict is written exactly as you see it here. Assign it the value you want before writing it.


推荐阅读