1

I have a firebase database that looks like this:

Firebase Database Example

I have the courseID variable stored in my $state.params.courseID parameter, and now I want to only pull Sections where the coursedID matches the $state.params.courseID variable, and then store the results in an array here is what I tried so far:

    var sectionsRef = firebase.child('Sections');
    sectionsRef
    .orderByChild('courseID')
    .on('value', function(snap) {
      console.log(snap.val(), 'Sections');
      console.log(snap.key(), 'Key');
    });
    $scope.sections = $firebaseArray(sectionsRef);

I'm really confused on how to do even basic queries like this in Firebase.

David East
  • 31,526
  • 6
  • 67
  • 82
Jordash
  • 2,926
  • 8
  • 38
  • 77

1 Answers1

3

You can use the equalTo() method in combination with orderByChild().

var sectionsRef = firebase.child('Sections');
sectionsRef
.orderByChild('courseID')
.equalTo('And now?');
$scope.sections = $firebaseArray(sectionsRef);

If you want to be slick with your routing, you can create a factory that retrieves the courses by courseID and resolve it in the router.

angular.module('app', ['firebase'])
  .config(ApplicationConfig)
  .constant('FBURL', 'https://<my-fb-app>.firebaseio.com/')
  .service('RootRef', ['FBURL', Firebase)
  .factory('Sections', Sections)
  .controller('SectionsCtrl', SectionsCtrl);

function Sections(RootRef, $firebaseArray) {
  var sectionRef = RootRef.child('sections');
  return {
    byCourseId: function byCourseId(value) {
      sectionsRef
        .orderByChild('courseID')
        .equalTo(value);
      return $firebaseArray(sectionsRef);
    }
  }
}

function ApplicationConfig($stateProvider) {
  $stateProvider
    .state("sections", {
      controller: "SectionsCtrl",
      templateUrl: "views/sections.html",
      resolve: {
        sections: function(Sections, $state) {
          return Sections.byCourseId($state.params.courseid);
        }
      }
    });
}

function SectionsCtrl($scope, sections) {
   // This is the resolved data in your controller
   $scope.sections = sections;
}
David East
  • 31,526
  • 6
  • 67
  • 82
  • Ok that worked, I stored the variable in the snapshot like this: `$scope.sections = snap.val();` How would I access the snapshot id value like Firebase provides in the `$firebaseArray()` method? – Jordash Nov 30 '15 at 00:57
  • 1
    You don't need to store anything in the `on` method. Just passing the ref to `$firebaseArray()` will keep the array synchronized for you. I update the answer to show how you can inject the desired courses into the controller with the `resolve()` method. – David East Nov 30 '15 at 00:59
  • It doesn't seem to be keeping the array synchronized, it just dumps all of the data into the scope variable, but if I assign the scope variable to the snap.val() it exports the correct data, it just doesn't pass through the id of the section anywhere (firebaseArray puts it in the $id) – Jordash Nov 30 '15 at 01:06
  • Nevermind, I had to change the way I was accessing the variable like this: `var sectionsRef = firebase.child('Sections') .orderByChild('courseID') .equalTo($state.params.courseID);` Thanks for the help, that makes a lot more sense – Jordash Nov 30 '15 at 01:09
  • 1
    No problem! `The $firebaseArray()` takes in the ref and then binds an array to all of the Firebase child events. So you don't need to call `on` because it's done for you. – David East Nov 30 '15 at 01:13
  • This worked but caused another problem, now I can't order the results by priority, see the question here: http://stackoverflow.com/questions/33992935/how-to-get-specific-element-and-orderby-priority-in-firebase-and-angular – Jordash Nov 30 '15 at 06:52