'How do I take the name of the Firebase document? Swift

How do I take the name of the Firebase document, directly from the information of the logged in user?

Basically I have a user collection and a Degree Course collection.

When I use the func GetCorsodiLaurea

I don't want to manually insert the document name in .document ("") But I would like to automatically take the name of the document directly from the user's info The field that declares which course is connected to the user is "TipoCorso". As you can see in the Degree Courses collection there is the value of the "TipoCorso" field

Here is the code of the function and a screen of the Firebase Database:

  import SwiftUI
  import Firebase
  import FirebaseFirestoreSwift
  import FirebaseDatabase


  class ProfileViewModel : ObservableObject{
@Published var userInfo = UserModel(Nome: "", Cognome: "", photoURL: "", Nomeintero: "", Corsodilaurea: "", Tipocorso: "")
@Published var userDegree = userDegreeModel(Name: "", TotalSubjects: "")

var ref = Firestore.firestore()
let uid = Auth.auth().currentUser!.uid
let db = Firestore.firestore()



init() {
    fetchUser()
    GetCorsodiLaurea()
}

func fetchUser() {
    
    ref.collection("users").document(uid).getDocument { [self] (doc, err) in guard let user = doc else { return }
    
        
        
        let Nome = user.data()?["Nome"] as! String
        let Cognome = user.data()?["Cognome"] as! String
        let photoURL = user.data()?["photoURL"] as! String
        let Nomeintero = user.data()?["Nomeintero"] as! String
        let Tipocorso = user.data()?["Tipocorso"] as! String
        let Corsodilaurea = user.data()?["Corsodilaurea"] as! String
        
        
        DispatchQueue.main.async {
            self.userInfo = UserModel(Nome: Nome, Cognome: Cognome, photoURL: photoURL, Nomeintero: Nomeintero, Corsodilaurea: Corsodilaurea, Tipocorso: Tipocorso)            }
    }
    
}

func GetCorsodiLaurea() {
    db.collection("DegreeCourses").document(self.userInfo.Tipocorso).getDocument { [self] (doc, err) in guard let DegreeCourses = doc else { return }
    let Name = DegreeCourses.data()?["Name"] as! String
    let TotalSubjects = DegreeCourses.data()?["TotalSubjects"] as! String
        
           // [END doc_reference]
           // [END collection_reference]
    DispatchQueue.main.async {
    self.userDegree = userDegreeModel(Name: Name, TotalSubjects: TotalSubjects)
    }
        
    }

    
}





}

User

DegreeCourses



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source