2

I have the following code in my controller to go to a firebase database node, loop through the records at that node and return a count of each of the status types into an array (to act as the data for a chartjs chart). The code also waits for changes (edits and deletions) and updates the array accordingly. This all works exactly as I want

var ref = new Firebase("https://my-firebase-url/")
ref.on('child_added', function (snapshot) {
    var status = snapshot.val().status.Status;
    updateStatusCount(status, addStatus);

    ref.child(snapshot.key()).on('child_changed', function (chsnap) {
        if (chsnap.key() == 'status') {
            var chstatus = chsnap.val().Status;
            updateStatusCount(status, removeStatus);
            updateStatusCount(chstatus, addStatus);
            status = chstatus;
        }
    });
    ref.child(snapshot.key()).on('child_removed', function (rmsnap) {
        if (rmsnap.key() == 'status') {
            updateStatusCount(status, removeStatus);
        }
    });

});

function addStatus(index) {
    $scope.data[0][index] = $scope.data[0][index] + 1;
}

function removeStatus(index) {
    $scope.data[0][index] = $scope.data[0][index] - 1;
}

function updateStatusCount(status, cb) {
    switch (status) {
        case 'Status 1':
            cb(0);
            // $scope.data[0][0] = $scope.data[0][0] + 1 
            break;
        case 'Status 2':
            cb(1);
            // $scope.data[0][1] = $scope.data[0][1] + 1 
            break;
        case 'Status 3':
            cb(2);
            // $scope.data[0][2] = $scope.data[0][2] + 1 
            break;
        case 'Status 4':
            cb(3);
            // $scope.data[0][3] = $scope.data[0][3] + 1 
            break;
        case 'Status 5':
            cb(4);
            // $scope.data[0][4] = $scope.data[0][4] + 1 
            break;
        default:
            break;
    }
}

});

If I amend my firebase reference to include .orderByChild and .startAt to filter the returned data

       var ref = new Firebase("https://my-firebase-url/")
           .orderByChild("location").startAt(London)  

I get the following warning in the console

FIREBASE WARNING: Exception was thrown by user callback. TypeError: ref.child is not a function

and the changes (edits and deletions) no longer update, I have to do manual page refresh for the correct figures to be displayed. I am really struggling to work out why this is happening when adding the filter.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Janbango
  • 191
  • 3
  • 15

1 Answers1

3

The Firebase Query class doesn't have a child() method, because... what's the child of a query?

One way to solve the syntax error, split the ref and the query into two variables:

var ref = new Firebase("https://my-firebase-url/");
var query = ref.orderByChild("location").startAt(London);
query.on('child_added', function (snapshot) {
   ...
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks Frank, makes sense that there is no such thing as a child of a query. I have split my ref and query into two variables but I get the same error with "query.child is not a function" warning. Am I missing something as even with the new query variable I am still trying to use the child() method? – Janbango Jul 04 '16 at 07:10
  • Ok, having a slow Monday morning. All works now based on your answer. Thanks! – Janbango Jul 04 '16 at 09:09