I have an Express JS application that allows users to upload 2 excel files, one a dependent of the other. I am using dropzone on the front end and everything seems to be working fine. However, the backend still needs some work. During the upload of the two files not all of the data is being processed correctly, sometimes the report is not inserted before the results start.
//main loop
//arrange req.files array so that reports.xlsx will be processed first since results is dependent on reports
if (req.files[0].originalname == "results.xlsx") move(req.files, 0, 1);
req.files.forEach(async file => {
let jsonPath = path.join(__dirname, "..", "uploads", file.filename);
let jsonString = fs.readFileSync(jsonPath, "utf8");
// Parse a file
let workSheetsFromFile = xlsx.parse(jsonPath);
let fieldNames = workSheetsFromFile[0].data[0];
let resultArray = workSheetsFromFile[0].data;
// number of rows of user data to process
let numOfRows = resultArray.length - 1;
let fileName = file.originalname.split(".")[0];
//check if field names are correct in both reports or results
error = checkFieldNames(fieldNames, fileName);
if (error) {
return res.status(422).send(error);
}
//check if the data is of the correct format in each row of reports or results
error = checkData(fileName, resultArray, numOfRows);
if (error) {
return res.status(422).send(error);
} else {
//there are no errors process data in reports or results
let doneUploading = await uploadData(fileName, resultArray, request, user, clientIP);
console.log("doneUploading", doneUploading);
return res.status(200).send(req.file);
}
});
doneUploading function
async function uploadData(fileName, resultArray, request, user, clientIP) {
if (fileName == "reports") {
//Loop Through the Records in the Report Table Array and insert the Uploaded
//table values into live table
for (let i = 1; i <= resultArray.length - 1; i++) {
var qryInsertReport = await request.query(`INSERT INTO tblLabReport ....`;);
}
}
else if (fileName == "results") {
for (let i = 1; i <= resultArray.length - 1; i++) {
var qryInsertResult = await request.query(`INSERT INTO tblLabResult ....;
}
var reportUpload = await request.query(`SELECT * FROM tblLabReport`);
var resultUpload = await request.query(`SELECT * FROM tblLabResult`);
console.log("reportUpload", reportUpload);
console.log("resultUpload", resultUpload);
}
//Attempt to Delete Files that were Uploaded
fs.readdir(DIRECTORY, (err, files) => {
if (err) throw err;
for (const file of files) {
fs.unlink(path.join(DIRECTORY, file), err => {
if (err) throw err;
});
}
});
console.log("Done checking and inserting reports");
return true;
}