简体   繁体   English

Imageurl 未保存到 Firestore 数据库 - SwiftUI

[英]Imageurl not getting saved to Firestore database - SwiftUI

So I'm building a user profile in my app and saving Name, Bio, Image, uid, and date created in Firestore database.因此,我在我的应用程序中构建了一个用户配置文件,并保存了在 Firestore 数据库中创建的名称、生物、图像、uid 和日期。 While other values are getting saved in the database and retrieved in user profile, the imageurl is getting saved as empty string, and hence not getting retrieved in the user profile.虽然其他值保存在数据库中并在用户配置文件中检索,但 imageurl 被保存为空字符串,因此不会在用户配置文件中检索。 Where am I going wrong here?我在哪里错了?

import SwiftUI
import Firebase

class RegisterViewModel : ObservableObject{
    
    @Published var name = ""
    @Published var bio = ""
    
    @Published var image_Data = Data(count: 0)
    @Published var picker = false
    let ref = Firestore.firestore()
    
    // loading view
    @Published var isLoading = false
    @AppStorage("current_status") var status = false
    
    func register(){
        
        isLoading = true
        // setting user data to firestore
        let uid = Auth.auth().currentUser!.uid
        
        UploadImage(imageData: image_Data, path: "profile_Photos") { (url) in
            
            self.ref.collection("Users").document(uid).setData([
                
                "uid": uid,
                "imageurl": url,
                "username": self.name,
                "bio": self.bio,
                "dateCreated": Date()
            
            ]) {(err) in
                
                if err != nil{
                    self.isLoading = false
                    return
                }
                self.isLoading = false
                // success means setting status as true
                self.status = true
            }
            
        }
        
    }
    
}


import SwiftUI
import Firebase

func UploadImage(imageData: Data, path: String, completion: @escaping (String) -> ()){
    
    let storage = Storage.storage().reference()
    let uid = Auth.auth().currentUser!.uid
    
    storage.child(path).child(uid).putData(imageData, metadata: nil) { (_, err) in
        
        if err != nil {
            
            completion("")
            return
            
        }
        
        // downloading url and sending back
        
        storage.child(path).child(uid).downloadURL { (url, err) in
            
            if err != nil {
                
                completion("")
                return
                
            }
            
            completion("\(url!)")
            
        }
        
    }
    
}

The result in Firestore database is something like: Firestore 数据库中的结果类似于:

bio: "Bio"生物:“生物”

dateCreated: September 22, 2022 at 1:32:43 AM UTC+5:30日期创建时间:2022 年 9 月 22 日凌晨 1:32:43 UTC+5:30

imageurl: ""图片网址:“”

uid: "o32JbCwShtKLhtHibcui474NwFA9" uid:“o32JbCwShtKLhtHibcui474NwFA9”

username: "Name"用户名:“姓名”

Swift's URL is not a supported type in Firestore. Firestore 中不支持 Swift 的URL类型。 You must convert the URL to a supported type, like String , before saving it.在保存之前,您必须将URL转换为受支持的类型,例如String

"imageurl": url!.absoluteString

You must then reconstruct the URL when you fetch the string value from Firestore.然后,当您从 Firestore 获取字符串值时,您必须重建URL

let imageurl = URL(string: imageurl)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM