Home > Blockchain >  SwiftUI: Value of type 'DocumentSnapshot?' has no member 'document'
SwiftUI: Value of type 'DocumentSnapshot?' has no member 'document'

Time:02-24

When trying to fetch a user, I am getting the error: "Value of type 'DocumentSnapshot?' has no member 'document'". The error occurs on the line that starts "let uid....".

import SwiftUI
import Firebase

// Global Refernce

let ref = Firestore.firestore()

func fetchUser(uid: String,completion: @escaping (UserModel) -> ()){
    
    ref.collection("Users").document(uid).getDocument { (doc, err) in
        guard let user = doc else{return}
        
        let username = user.data()?["username"] as? String ?? "No Username"
        let pic = user.data()?["imageurl"] as? String ?? "No image URL"
        let bio = user.data()?["bio"] as? String ?? "No bio"
        let uid = doc.document.data()["uid"] as! String ?? 
        
        DispatchQueue.main.async {
            completion(UserModel(username: username, pic: pic, bio: bio, uid: uid))
        }
    }
}

CodePudding user response:

if uid is a field like others username and imageurl then use

let uid = user.data()["uid"] as? String ?? ""

if you have made that uid as id of the document then use

let uid = doc.documentID

See Docs

  • Related