【问题标题】:Iterate over Async Function with For Loop (db query, Node.js)使用 For 循环迭代异步函数(数据库查询,Node.js)
【发布时间】:2016-06-17 05:51:08
【问题描述】:

我在项目的最后一部分,我正在尝试将来自 json 的数据插入到我的 MySQL 数据库中,这是我的示例数据

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

我的函数正在解析样本数据(抱歉,函数太长了)

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

    }
}

我的函数工作正常,但是当我检查我的数据库时,VoteCount 部分的数据有误。

预期

e ejercito 4
j ejercito 5

但在数据库中是

结果

e ejercito 5
j ejercito 5

如果我的查询尚未完成,如何停止我的 for 循环?

【问题讨论】:

  • 您不能“暂停”for 循环以等待异步操作。 for 循环是同步的。相反,您将不得不手动迭代,将迭代放入函数中,并仅在异步操作完成时手动开始下一次迭代。
  • @jfriend00 你能给我举个例子吗?这是我在 nodejs (javascript) 中的第一个月

标签: mysql node.js asynchronous callback promise


【解决方案1】:

for循环不用停,JS有一种异步的美。

事情是,当module.exports.insertVotes(prid, ref, c, dataa.id, function(res){}); 被执行时,for 循环已经通过,所以你最终得到了所有情况下的 las 索引。

要解决此问题,您可以使用 forEach loop 。每次迭代都有自己的范围。

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

    });


}

【讨论】:

  • 谢谢!你能解释一下 foreach 和 for-loop 的区别吗?这样我就可以再次应用它了
  • 乐于助人!要了解这里发生了什么,您需要了解一些基本的 JS 概念。我将从 JS 作用域开始 toddmotto.com/… >> 回调
猜你喜欢
  • 1970-01-01
  • 2013-08-04
  • 1970-01-01
  • 2019-05-27
  • 2015-04-11
  • 2014-07-04
  • 1970-01-01
  • 2018-09-04
  • 2016-07-10
相关资源
最近更新 更多