如何使用for循环在Firebase中使用反向数据?

时间:2015-09-22 00:15:10

标签: angularjs for-loop foreach firebase

所以我有一个注册控制器来检查用户的注册码是否有效。在SignUp函数中,我有一个名为validGroupCode的全局变量来标记代码是否有效。如果输入组代码与Firebase中的现有代码匹配,则将其设置为true。控制台的日志显示了forEach中的validGroupCode为true,但是,在控制台的日志中,validGroupCode在forEach()函数之外始终为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");
        }
    }    
};

}])

1 个答案:

答案 0 :(得分:0)

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