【问题标题】:How to use retrive data in Firebase by using for loop?如何使用 for 循环在 Firebase 中检索数据?
【发布时间】:2015-09-22 00:15:09
【问题描述】:

所以我有一个注册控制器来检查用户的注册码是否有效。在 SignUp 函数中,我有一个名为 validGroupCode 的全局变量来标记代码是否有效。如果输入组代码与 Firebase 中的现有代码匹配,我将其设置为 true。控制台的日志显示 forEach 内的 validGroupCode 为 true,但是,在控制台的日志中,forEach() 函数外的 validGroupCode 始终为 false。原来 forEach() 是一个同步操作,那么如何使用 for 循环来检索数据呢?

// Register controller
.controller('RegisterCtrl', ['$scope', '$firebaseArray', '$location', 'CommonProp', '$firebaseAuth', function($scope, $firebaseArray, $location, CommonProp, $firebaseAuth) {
$scope.signUp = function() {

    var firebaseObj = new Firebase("xxxx.com/");

    var firebaseGroupObj = new Firebase("hxxxebaseio.com/Groups");

    var validGroupCode = false;

    // Sign up implementation 
    if (!$scope.regForm.$invalid) {

        // var groupQuery = firebaseGroupObj.orderByChild('code').equalTo(groupcode);
        firebaseGroupObj.on("value", function(allGroupCodes){
            allGroupCodes.forEach(function(codeValue) {
                var existingCode = codeValue.child("code").val();

                if (existingCode === groupcode){

                    validGroupCode = true;

                    if(email && password && validGroupCode) {
                        auth.$createUser({email, password})
                            .then(function(userData) {
                                // add user
                            });
                    }
                }        
            });

        });
        if(!validGroupCode){
            console.log("Group Code is not correct");
        }
    }    
};

}])

【问题讨论】:

  • 我想我知道该怎么做了。为了防止同步,我只是使用了 $loaded() 函数,将以上所有内容放入这个函数中,然后一切正常! $scope.data.$loaded() .then(function() { console.log($scope.data); }) .catch(function(err) { console.error(err); });

标签: angularjs for-loop foreach firebase


【解决方案1】:

for 是一个异步操作。如果您希望结果同步到下一个代码,它应该与

一起使用
// Register controller
.controller('RegisterCtrl', ['$scope', '$firebaseArray', '$location', 'CommonProp', '$firebaseAuth', function($scope, $firebaseArray, $location, CommonProp, $firebaseAuth) {
$scope.signUp = function() {

    var firebaseObj = new Firebase("xxxx.com/");

    var firebaseGroupObj = new Firebase("hxxxebaseio.com/Groups");

    var validGroupCode = false;

    // Sign up implementation 
    if (!$scope.regForm.$invalid) {

        // var groupQuery = firebaseGroupObj.orderByChild('code').equalTo(groupcode);
        firebaseGroupObj.on("value", function(allGroupCodes){

           /////For instead of forEach, for sync operation,
           /////so the if(!validGroupCode) will come after the for loop

           for(var index in allGroupCodes){
                var codeValue = allGroupCodes[index];
                var existingCode = codeValue.child("code").val();

                if (existingCode === groupcode){

                    validGroupCode = true;

                    if(email && password && validGroupCode) {
                        auth.$createUser({email, password})
                            .then(function(userData) {
                                // add user
                            });
                    }
                }        
            });
        }
        if(!validGroupCode){
            console.log("Group Code is not correct");
        }
    }    
};

【讨论】:

  • 谢谢,里奥!这是有道理的。但是 Firebase 使用 forEach 从 Datasnapshot 中检索数据,我不知道如何使用 for Loop 来完成它。我试过你的代码,但它告诉我 codeValue.child 不是一个函数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-16
  • 1970-01-01
相关资源
最近更新 更多