javascript - 在for循环中调用函数,异步问题
问题描述
我正在尝试在我的 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!
解决方案
在您的示例中,正在插入文件,但每次循环迭代都没有等待插入文件。如果您希望控制台日志看起来像您描述的那样,您需要在回调后插入下一个文件。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);
但是,当然,正如评论中所指出的那样,SQL 插入命令的编写方式可能存在其他问题。
推荐阅读
- ios - 有没有办法将 iPhone 用作 iPad 应用程序的小键盘?
- dictionary - 将模型查看器中的光照添加到 AR.js 场景中
- reactjs - setState 在 React 中太滞后了
- google-sheets - 在 ArrayFormula 中使用 Min 和 Max 函数
- php - 如何将数字转换为小数点后始终有 2 位数字?
- javascript - 对机器人自己的消息使用 bulkDelete() 时缺少权限
- java - 用于截断链表的递归解决方案
- regex - 查询以在 Google 工作表中选择和显示多列?
- c# - 如何使用 where 使这个 SQL 语句工作
- r - 如何使用 gtsummary::tbl_regression() 报告随机效应的方差估计