【问题标题】:Firebase - Query ChainsFirebase - 查询链
【发布时间】:2017-03-21 02:35:08
【问题描述】:

所以我在这里有一个有趣的问题。我们的软件包含一项功能,该功能允许基于销售代表、经理之间的层次结构以及我们的用户在使用我们的软件时希望支持的层级进行动态佣金分配。用户可能看起来像这样:

$uid
    fname
    lname
    lineage
         fatherlink
               $uid_of_another_user
         childlink
               $uid_of_another_user
               $uid_of_another_user

好的,现在假设childlink 对象中的每个孩子都包含另外两个孩子。例如,我希望能够从经理开始,并创建一个包含他所有孩子的数组,以及他孩子的孩子等等......由于我们支持任意数量的管理级别,因此查询给定下的所有孩子用户需要是动态的。这就是我们现在所拥有的。

var children = [];
        _.child('user/' + $localStorage.currentUser.uid + '/lineage/child_link').on('value', function(sn1) {
            sn1.forEach(function(sn2) {
                _.child('user/' + sn2.key + '/lineage/child_link/').on('value', function(sn3) {
                    sn3.forEach(function(sn4) {
                        _.child('user/' + sn4.key + '/lineage/child_link/').on('value', function(sn5) {
                            sn5.forEach(function(sn6) {
                                _.child('user/' + sn6.key + '/lineage/child_link/').on('value', function(sn7) {
                                    sn7.forEach(function(sn8) {
                                        _.child('user/' + sn8.key + '/lineage/child_link/').on('value', function(sn9) {
                                            sn9.forEach(function(sn11) {
                                                _.child('user/' + sn11.key + '/lineage/child_link/').on('value', function(sn12) {
                                                    sn12.forEach(function(sn13) {
                                                        children.push(sn13.key)
                                                    })
                                                });
                                                children.push(sn11.key)
                                            })
                                        });
                                        children.push(sn8.key)
                                    })
                                });
                                children.push(sn6.key)
                            })
                        });
                        children.push(sn4.key)
                    })
                });
                children.push(sn2.key)
            })
        });
        return children;

显然,问题在于这只查询 6 层深度。有任何想法吗?甚至可能?

【问题讨论】:

  • 有没有可能,试一试,看看能不能行。这段代码是否合理/可行,嗯……你似乎已经知道了,否则你就不会在这里。那么你真正的问题是什么?是如何递归地做到这一点?你关心性能吗?如果是这样,您是否已经尝试过?结果与您的预期相比如何?
  • AngularJS 程序员避免使用chaining promisesPyramid of Doom (AKA Callback Hell)。将基于异步回调的操作转换为 Promise 并它们。
  • 完美就是我所需要的。不知道为什么我没想到!

标签: angularjs node.js firebase firebase-realtime-database


【解决方案1】:

感谢@georgeawq,这是我们的解决方案:

    children: function(callback) {
        function getChildren(a) {
            _.child("user/" + a + "/lineage/child_link/").once("value", function(a) {
                null != a.val() ? a.forEach(function(a) {
                    children.push(a.key), getChildren(a.key)
                }) : children.sort(); callback(children)
            })
        }
        var children = [];
        getChildren($localStorage.currentUser.uid);
    },

基本但完美的解决方案。谢谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-12
    • 2020-08-19
    • 1970-01-01
    • 2021-01-15
    • 2020-09-16
    • 2021-11-17
    相关资源
    最近更新 更多