something
something

Reputation: 99

Iterate over Async Function with For Loop (db query, Node.js)

Im in the last part of my project and I'm trying to insert data from json in my MySQL db here are my sample data

{"data": 
[{"cpos": "g", "cfname": "e", "clname": "ejercito", "cvcount": "4"}, 
{"cpos": "g", "cfname": "j", "clname": "ejercito", "cvcount": "5"}]}

and that sample data is being parsed by my function (sorry for long function)

checkPositionCandidateInsertVote: function(ref, prid, json, callback){
    var len = json.data.length;
    for (var i = 0; i < len; i++) {
        var fn = (json.data[i].cfname).toLowerCase();
        var ln = (json.data[i].clname).toLowerCase();
        var c = json.data[i].cvcount;
        console.log(fn, ' ', c);
        switch((json.data[i].cpos).toLowerCase()){
            case "g":
                module.exports.getCandiName(fn, ln, "Governor", function(dataa){
                    //dataa.id
                    module.exports.insertVotes(prid, ref, c, dataa.id, function(res){
                    });
                });
                break;
            case "vg":
                module.exports.getCandiName(fn, ln, "Vice Governor", function(dataa){
                    //dataa.id
                    module.exports.insertVotes(prid, ref, c, dataa.id, function(res){
                    });
                });
                break;
            case "m":
                module.exports.getCandiName(fn, ln, "Mayor", function(dataa){
                    //dataa.id
                    module.exports.insertVotes(prid, ref, c, dataa.id, function(res){
                    });
                });
                break;
            case "vm":
                module.exports.getCandiName(fn, ln,  "Vice Mayor", function(dataa){
                    //dataa.id
                    module.exports.insertVotes(prid, ref, c, dataa.id, function(res){
                    });
                });
                break;
            case "bm":
                module.exports.getCandiName(fn, ln, "Board Member", function(dataa){
                    //dataa.id
                    module.exports.insertVotes(prid, ref, c, dataa.id, function(res){
                    });
                });
                break;
            case "cg":
            case "cm":
            case "cw":
                module.exports.getCandiName(fn, ln, "Congressman", function(dataa){
                    //dataa.id
                    module.exports.insertVotes(prid, ref, c, dataa.id, function(res){
                    });
                });
                break;
            case "c":
                module.exports.getCandiName(fn, ln, "Councilor", function(dataa){
                    //dataa.id
                    module.exports.insertVotes(prid, ref, c, dataa.id, function(res){
                    });
                });
                break;
        }

    }
}

My function/s are working fine but when I check my db the data are wrong in VoteCount part.

Expected

e ejercito 4
j ejercito 5

but in the db is

Result

e ejercito 5
j ejercito 5

How to stop my for loop if my query is not yet finished?

Upvotes: 1

Views: 1338

Answers (1)

oKonyk
oKonyk

Reputation: 1476

There is no need to stop for loop, there is a beauty of asynchronous nature of JS.

Thing is that by the time module.exports.insertVotes(prid, ref, c, dataa.id, function(res){}); is executed, for loop already went through, so you end up with las index for all cases.

To fix that you can use forEach loop . Each iteration will have its own scope.

    checkPositionCandidateInsertVote: function(ref, prid, json, callback){

    json.data.forEach(function(listItem, i){

        var fn = (json.data[i].cfname).toLowerCase();
        var ln = (json.data[i].clname).toLowerCase();
        var c = json.data[i].cvcount;
        console.log(fn, ' ', c);
        switch((json.data[i].cpos).toLowerCase()){
            case "g":
                module.exports.getCandiName(fn, ln, "Governor", function(dataa){
                    //dataa.id
                    module.exports.insertVotes(prid, ref, c, dataa.id, function(res){
                    });
                });
                break;
            ...
            ...
            ...

            case "c":
                module.exports.getCandiName(fn, ln, "Councilor", function(dataa){
                    //dataa.id
                    module.exports.insertVotes(prid, ref, c, dataa.id, function(res){
                    });
                });
                break;
        }

    });


}

Upvotes: 1

Related Questions