Reputation: 41
How to retrieve data from the Realtime database? I want to get user specific data search by uid witch is also a child of the user database. I need retrieve all data for specific user ID "day","month"etc. values as variables.
I've got data in firebase put by:
DatabaseRow class:
data class DatabaseRow (
val id:String = "",
val names: String= "",
val lastname: String="",
val day: String="",
val month: String="",
val year:String=""
)
firebase input:
val firebaseInput = DatabaseRow(user,names,lastname,day,month,year)
userRef.child("$user").setValue(firebaseInput)
to retrieve data I'm using this code:
auth = FirebaseAuth.getInstance()
FirebaseUser = auth.getCurrentUser()!!
val uid = auth.getUid()!!
val fireBase = FirebaseDatabase.getInstance()
userRef = fireBase.getReference("users")
val ordersRef = userRef.child("$uid").equalTo("$uid")
val valueEventListener = object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
for (ds in dataSnapshot.children) {
val username = ds.child("day").getValue(String::class.java)
Log.d("DataBaseGetName",username)
[email protected] = username
}
}
override fun onCancelled(databaseError: DatabaseError) {
Log.d("Data", databaseError.getMessage()) //Don't ignore errors!
}
}
ordersRef.addValueEventListener(valueEventListener)
that are database rules:
{
"rules": {
".read": true,
".write": true,
"users":{
"$uid":{
".write": "auth.uid === $uid",
".read": "auth.uid === $uid"
},
"users":{
".indexOn": [".value","id","names","lastname","day","month","year"]
},
}
}
}
Upvotes: 1
Views: 2450
Reputation: 6919
When you're receiving the uid
, you're trying to compare it with the database uid
. The uid
is always different.
val ordersRef = userRef.child("$uid")
val valueEventListener = object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
val username = dataSnapshot.child("names").getValue(String.class);
Log.d("DataBaseGetName",username)
}
override fun onCancelled(databaseError: DatabaseError) {
Log.d("Data", databaseError.getMessage()) //Don't ignore errors!
}
}
ordersRef.addValueEventListener(valueEventListener)
Upvotes: 3