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?

3
  • 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. Commented Mar 3, 2016 at 18:31
  • @jfriend00 can you give me some example? this is my first month in nodejs (javascript) Commented Mar 3, 2016 at 18:35
  • Here's are some examples: stackoverflow.com/questions/29880715/… and stackoverflow.com/questions/34191788/… Commented Mar 3, 2016 at 18:38

1 Answer 1

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

    });


}
Sign up to request clarification or add additional context in comments.

2 Comments

thankyouu! can you explain the difference of foreach and for-loop? so that I can apply it of the same occur again
Happy to help! To understand what going on here, you need to understand few basic JS concepts. I would start with JS scoping toddmotto.com/… >> callbacks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.