skip to Main Content

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?

2

Answers


  1. 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());
      }
    });
    
    Login or Signup to reply.
  2. 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())
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search