在for循环中调用函数,引发问题

时间:2019-04-03 22:53:12

标签: javascript node.js

我试图在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!

1 个答案:

答案 0 :(得分: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);

但是,当然,正如注释中所指出的那样,可能有other issues的SQL插入命令是这样写的。