【问题标题】:Calling a function within a for loop, asynch problem在for循环中调用函数,异步问题
【发布时间】:2019-04-03 22:53:12
【问题描述】:

我正在尝试在我的 FILE 表中插入一行,但是当我尝试这样做时,事情完成的顺序并不是我想要的。我知道 JS 是异步的,但我不确定这会如何影响我在循环中的工作方式。

一些背景知识,sharedLib.serverCreateCalendar(file) 只需要一个 .ics 文件并将其内容转换为 JSON,这样我就可以从中获取信息

我对 JS 或 SQL 没有太多经验,所以我不知道应该怎么做才能解决这个问题。

for (var i = 0; i < files; i++) {
  cals = sharedLib.serverCreateCalendar(file); // get the cal info as a JSON

  var obj, fileN, version, prodID, evtNum; //variables for parsing the JSON and getting the values
  var obj = JSON.parse(cals); //parse the calendar JSON

  //Assign the values i want
  fileN = files[j];
  version = obj.version;
  prodID = obj.prodID;
  evtNum = obj.numEvents;

  //SQL to insert into the FILE table
  var calInsert = ("INSERT INTO FILE (file_Name, version, prod_id) VALUES('" + fileN + "'," + version + ",'" + prodID + "'" + ");"); //This is the SQL to insert into the FILE table with its required info
  console.log(calInsert);

  //Send the SQL query to add a file
  connection.query(calInsert, function(err, result) {
    if (err) { //error inserting into the DB
      console.log("Calendar insert error");
      res.send("Error");
    } else { //if placing the calendar is good then add the events 
      console.log("Added the calendar!");
    }
  });

}

理想情况下会输出

("INSERT INTO FILE (file_Name, version, prod_id) VALUES('" + fileN + "'," + version + ",'" + prodID + "'" + ");"); 
Added the calendar!

("INSERT INTO FILE (file_Name, version, prod_id) VALUES('" + fileN + "'," + version + ",'" + prodID + "'" + ");"); 
Added the calendar!

("INSERT INTO FILE (file_Name, version, prod_id) VALUES('" + fileN + "'," + version + ",'" + prodID + "'" + ");"); 
Added the calendar!

依此类推,直到我完成循环,但是它会输出这个

("INSERT INTO FILE (file_Name, version, prod_id) VALUES('" + fileN + "'," + version + ",'" + prodID + "'" + ");"); 
("INSERT INTO FILE (file_Name, version, prod_id) VALUES('" + fileN + "'," + version + ",'" + prodID + "'" + ");"); 
("INSERT INTO FILE (file_Name, version, prod_id) VALUES('" + fileN + "'," + version + ",'" + prodID + "'" + ");"); 
Added the calendar!
Added the calendar!
Added the calendar!

【问题讨论】:

  • 这是在一个封闭的网络上,用于分配。这是我第一次做任何 SQL,显然它不会很好。如果你能帮忙不攻击就好了
  • 查询都按照你的意愿执行,只是输出令人困惑。
  • 如果您真的想按顺序而不是并行执行查询,请参阅stackoverflow.com/questions/1151598/…。这是关于来自浏览器的 AJAX 调用,但对于任何迭代的异步操作,原理都是相同的。
  • @Barmar 哦,好吧!当我现在查看表格时,我可以看到这一点

标签: javascript node.js


【解决方案1】:

在您的示例中,正在插入文件,但每次循环迭代都没有等待插入文件。如果您希望控制台日志看起来像您描述的那样,您需要在回调后插入下一个文件。您可以通过完全删除 for 循环来实现:

var files = []; // Get an array of files some how

function insertFiles(theFiles) {
    // Handle end condition
    if (theFiles === undefined || theFiles.length == 0) {
        return;
    }
    var cals = sharedLib.serverCreateCalendar(file); // get the cal info as a JSON
    var obj = JSON.parse(cals); //parse the calendar JSON
    var fileN = theFiles.shift();   // Get the first file and remove from array
    var version = obj.version;
    var prodID = obj.prodID;
    var evtNum = obj.numEvents;

    //SQL to insert into the FILE table
    var calInsert = ("INSERT INTO FILE (file_Name, version, prod_id) VALUES('" + fileN + "'," + version + ",'" + prodID + "'" + ");"); //This is the SQL to insert into the FILE table with its required info
    console.log(calInsert);

    //Send the SQL query to add a file
    connection.query(calInsert, function(err, result) {
        if (err) { //error inserting into the DB
            console.log("Calendar insert error");
            res.send("Error");
        } else { //if placing the calendar is good then add the events 
            console.log("Added the calendar!");
            insertFiles(theFiles);
        }
    });
}

insertFiles(files);

但是,当然,正如 cmets 中所指出的那样,other issues 可能会以这种方式编写 SQL 插入命令。

【讨论】:

    猜你喜欢
    • 2011-12-03
    • 2017-12-16
    • 2012-10-31
    • 2015-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多