This code is currently in a View Controller for a login handler using Firebase. A string is written to a realtime database and the retrieveUserType function reads this data. Currently when trying to print the userType after the IBAction for the login button is pressed the value of the variable remains a blank string.
var userType:String = ""
func retrieveUserType(){
let userTypeDB = Database.database().reference()
let currentUser = "kLxqZteRfBeC0bNIkLCjrPukMGx1"
userTypeDB.child("UserType").child(currentUser).observeSingleEvent(of: .value, with: {
(snapshot) in
// Get user value
let value = snapshot.value as? NSDictionary
let email = value?["Email"] as? String ?? ""
self.userType = value?["User Type"] as? String ?? ""
print(email)
}){
(error) in
print(error.localizedDescription)
}
}
@IBAction func loginButtonPressed(_ sender: Any) {
retrieveUserType()
if userType != "Student"{
print("error")
} else {
print("success")
}