After using snapshot.exists(), username will always return as available upon signup. How can I alter my code to properly check whether or not a username has already been taken?
Here is a snippet of the json under "users" in the database:
{
"UserID#" : {
"credentials" : {
"name" : "testuser",
},
}
}
My code currently looks like:
let usersDB = Database.database().reference()
var taken = false
usersDB.child("users").child("credentials").queryOrdered(byChild:"name").queryEqual(toValue: username.lowercased()).observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.exists() {
taken = true
print("Username is not available.")
usernameAlert()
} else {
print("User is available")
}
No matter if the username is already taken on the database, the snapshot will say it does not exist.
Solutions I've tried with no success: check if the username exist in Firebase Query users in Firebase to check if username exists during sign up process Firebase querying for unique Username swift Check if user exists with username using Swift and Firebase Swift & Firebase | Checking if a user exists with a username