I'm trying to return a name after getting it on Firestore, but for some reason it's not working.
Here's my code:
func getName() -> String {
var name = ""
db.collection("users").whereField("email", isEqualTo: user.email!).getDocuments { (snapshot, error) in
if error != nil {
print(error!)
} else {
for document in (snapshot?.documents)! {
name = document.data()["name"] as! String
// if I add `print(name) here, it works.`
}
}
}
return name
}
But it returns an empty string :/ I want to return the actual name. How do I fix this?