“ref.child 不是函数"添加 .orderByChild.StartAt 过滤器时 [英] "ref.child is not a function" when adding .orderByChild.StartAt filter

查看:15
本文介绍了“ref.child 不是函数"添加 .orderByChild.StartAt 过滤器时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的控制器中有以下代码可以转到 firebase 数据库节点,遍历该节点上的记录并将每个状态类型的计数返回到一个数组中(作为 chartjs 图表的数据).该代码还等待更改(编辑和删除)并相应地更新数组.这一切都如我所愿

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;
    }
}

});

如果我修改我的 firebase 引用以包含 .orderByChild 和 .startAt 来过滤返回的数据

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 警告:用户回调引发了异常.类型错误:ref.child 不是函数

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.

推荐答案

Firebase Query 类没有 child() 方法,因为...查询的子级是什么?

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

一种解决语法错误的方法,将ref和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) {
   ...

这篇关于“ref.child 不是函数"添加 .orderByChild.StartAt 过滤器时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆