I'm trying to search for user email in my database and then when that email is found store the uid or the parent table (same as the uid) to the email as a val. At them moment i think i have the method searching for the email in the database so in the if statement i need to store the uid for later use.
fun checkUid(layout: View) {
val email = layout.findViewById<EditText>(ie.wit.savvytutor.R.id.loginEmail).text.toString()
val userDatabase =
FirebaseDatabase.getInstance("*db link")
.getReference(
"Users"
)
val emailCheck = userDatabase.equalTo(email)
val eventListener: ValueEventListener = object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
if (!dataSnapshot.exists()) {
//get The Uid for that user
println("yay")
}
}
override fun onCancelled(databaseError: DatabaseError) {
println("no!")
}
}
emailCheck.addListenerForSingleValueEvent(eventListener)
}
The json for my database looks like this:
{
"email" : "[email protected]",
"password" : "Test123",
"profilepic" : "",
"role" : "Parent",
"uid" : "-Mz1FR2jJnrBa1Ws-aVU"
}
So to clarify because i feel this is badly worded, i need to search the database for an email -> then get the uid or parent parent table name to that email -> and store that as a val. As mentioned I think i have the first part wokring!
CodePudding user response:
You're missing an orderBy
clause here:
val emailCheck = userDatabase.equalTo(email)
Since you want to compare the value of the email
property, you need to tell the database to order on that:
val emailCheck = userDatabase.orderByChild("email").equalTo(email)
Now the query will match the correct child nodes. For more on this, also see the documentation on ordering and filtering data.
You can then log the key of the matching child nodes with:
override fun onDataChange(dataSnapshot: DataSnapshot) {
for (snapshot in dataSnapshot.children) {
println(snapshot.key)
}
}
For more on this, see the documentation on listening for value events.