-1

I am trying to insert a row into my FILE table, but when I attempt this the order in which things are done isn't what I want it to be. I know that JS is asynchronous but, I'm not sure how that affects how I should work within a loop.

Some background, sharedLib.serverCreateCalendar(file) just takes a .ics file and converts its content to a JSON so i can get the info out of it

I don't have much experience with JS or SQL so I don't have any idea what I should do to fix this.

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!");
    }
  });

}

Ideally this would output

("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!

and so on until i'm done looping but, it outputs this instead

("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!
Barmar
  • 596,455
  • 48
  • 393
  • 495
Clayton D
  • 47
  • 7
  • 1
    this is on a closed network, for an assignment. This is my first time doing any SQL obviously its not going to be great. It'd be nice if you'd help not attack – Clayton D Apr 03 '19 at 22:57
  • 1
    The queries are all being executed as you intend, it's only the output that's confusing. – Barmar Apr 03 '19 at 23:00
  • If you really want to execute the queries sequentially, rather than in parallel, see https://stackoverflow.com/questions/1151598/how-to-make-all-ajax-calls-sequential. It's about AJAX calls from a browser, but the principle is the same for any iterated async action. – Barmar Apr 03 '19 at 23:03
  • @Barmar oh, okay! I can see that when I look at the tables now – Clayton D Apr 03 '19 at 23:04

1 Answers1

1

In your example the files are being inserted but each loop iteration is not waiting for the file to be inserted. If you want your console logs to look like you describe you need to insert the next file after the call back. You can achieve this by removing the for loop altogether:

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

But, of course, as has been pointed out in the comments there may be other issues with SQL insertion command written the way it is.

Jorge Cabot
  • 161
  • 2
  • 9