1

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?

oKonyk
  • 1,468
  • 11
  • 16
something
  • 99
  • 12
  • You can't "pause" a `for` loop to wait for an async operation. A `for` loop is synchronous. Instead, you will have to iterate manually, putting an iteration into a function and starting the next iteration manually only when your async operation completes. – jfriend00 Mar 03 '16 at 18:31
  • @jfriend00 can you give me some example? this is my first month in nodejs (javascript) – something Mar 03 '16 at 18:35
  • Here's are some examples: http://stackoverflow.com/questions/29880715/how-to-synchronize-a-sequence-of-promises/29906506#29906506 and http://stackoverflow.com/questions/34191788/how-to-process-a-big-array-applying-a-async-function-for-each-element-in-nodejs/34191957#34191957 – jfriend00 Mar 03 '16 at 18:38

1 Answers1

1

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

    });


}
oKonyk
  • 1,468
  • 11
  • 16
  • thankyouu! can you explain the difference of foreach and for-loop? so that I can apply it of the same occur again – something Mar 03 '16 at 23:28
  • Happy to help! To understand what going on here, you need to understand few basic JS concepts. I would start with JS scoping https://toddmotto.com/everything-you-wanted-to-know-about-javascript-scope/ >> callbacks – oKonyk Mar 04 '16 at 01:02