“ref.child is not a function” when adding .orderByChild.StartAt filter

不问归期 提交于 2020-01-15 05:01:11

问题


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.


回答1:


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) {
   ...


来源:https://stackoverflow.com/questions/38173359/ref-child-is-not-a-function-when-adding-orderbychild-startat-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!