Home > Net >  How can I get a specific data in Firebase (Kotlin)
How can I get a specific data in Firebase (Kotlin)

Time:09-06

I need to retrieve the "administrator" value from Firebase the Firebase database

This is the code I use:

FirebaseDatabase.getInstance().getReference("/restaurants/$restaurantUID/administrator")
            .get().addOnSuccessListener {

            Log.d("key", it.toString())
        }

The result in the log is this though: 2022-09-05 17:07:32.271 2885-2885/com.example.menuapp D/key: DataSnapshot { key = administrator, value = null }

How can I get the actual value for "administrator" and not a null?

CodePudding user response:

Try this -

FirebaseDatabase.getInstance().getReference("/restaurants/$restaurantUID").orderByChild("administrator").equalTo(“Alr6tf3nzTNTXbx53ysbcAxcM643”).addChildEventListener(new ChildEventListener() {
  @Override
  public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {
    System.out.println(dataSnapshot.getKey());
  }
});

CodePudding user response:

According to your last comment, I see that value of restaurantUID is:

eefef966-f227-45e8-b875-f568b87ee1fc

Which is totally different than the one in the database. The one in the database strats with "c20f02b0-...", hence that null for the value of administrator filed. If you want to get the correct value, then always make sure you're reading the data on the correct path:

val restaurantUID = "eefef966-f227-45e8-b875-f568b87ee1fc"
val db = FirebaseDatabase.getInstance().reference
val adminRef = db.child("restaurants/$restaurantUID/administrator")
adminRef.get().addOnSuccessListener {
    Log.d("key", it.toString())
}
  • Related