0

I am trying to find a session with a specific id (not key) without knowing the user id.

Database Structure

Database Structure

I had an initial attempt by looping through all the children of users and creating a new reference every time but it is not working and I can't figure out why nor I know if it should work in the first place.

 database.child("users").addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for(DataSnapshot snap : dataSnapshot.getChildren()){
                        database.child("users").child(snap.getKey()).child("sessions")
                                .orderByChild("id").equalTo(id)
                                .limitToFirst(1)
                                .addListenerForSingleValueEvent(new ValueEventListener() {
                                    @Override
                                    public void onDataChange(DataSnapshot data) {
                                        if (data.exists()) {
                                            Session session = data.getValue(Session.class);
                                        }
                                    }

                                    @Override
                                    public void onCancelled(DatabaseError databaseError) {

                                    }
                        });
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
1
  • You may use log to be more specific where your issue is Commented May 10, 2017 at 22:36

2 Answers 2

1

The whole idea of what you are trying to do is correct but you have a single mistake. Your last reference is wrong. When you are trying to iterate the second time you are missing a child. You need to add .child(sessionId). To solve this, please use this code:

database.child("users").child(snap.getKey()).child("sessions").child(sessionId)
                            .orderByChild("id").equalTo(id)
                            .limitToFirst(1)

In which sessionId is the second unique id generated by the push() method.

Hope it helps.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help. The whole point of what I was trying to do is that I don't have the sessionId. I only have the more user friendly id, which is not the same thing, and I'm trying to find sessionId (well actually get the whole session).
You have the sessionId, as i see it in your database. Is the unique key under the sessions node. To get it, use the getKey() on your reference in the exact moment when you are using the second push() method. Having that sessionId, you'll be able to use it in your DatabaseReference.
0

Issue caused by permission denied from Firebase.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.