2

We are migrating an app from Parse to Firebase and I'd like to build a UI where I can show the student name along with the classroom that s/he belongs to from the following Firebase data structure. For example, the class room name for aluck is 10th Grade.

{
    "students": {
        "aluck": {
            "firstName": "Andrew",
            "lastName": "Luck",
            "classroom": "-KJlJhyNH6ImKke-qAd-"
        },
        "bmorino": {
            "firstName": "Beth",
            "lastName": "Morino",
            "classroom": "-KJlJhyNH6ImKke-qAd-"
        }
    },
    "classRooms": {
        "-KJlJhyNH6ImKke-qAd-": {
            "location": "Down stairs",
            "name": "10th Grade",
            "students": {
                "aluck": true,
                "bmorino": true,
            }
        }
    }
}

My assumption was when I do something like this using the Firebase JavaScript SDK,

var db = firebase.database(); 
db.ref("students/aluck").on("value", function(snap) { 
    // success method will expose the classroom object with it 
}); 

it will expose the classRooms object, but it does not.

In Parse, I was able to do

var query = new Parse.Query(Student);
query.include("ClassRoom");

and it'll expose both the Student and ClassRoom objects.

Any help is appreciated.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

3

The Firebase Database does not support automatic joining of students and classrooms. You will have to handle this in you own code.

var db = firebase.database(); 
db.ref("students/aluck").on("child_added", function(student) { 
    db.ref("classRooms").child(student.child("classroom").val()).once("value", function(classroom) {
        console.log(classroom.val());
    });
}); 

In case you're worried about the performance of such once calls, see: Speed up fetching posts for my social network app by using query instead of observing a single event repeatedly

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807