我有一个Express JS应用程序,该应用程序允许用户上传2个excel文件,一个相互依赖。我在前端使用dropzone,似乎一切正常。但是,后端仍然需要一些工作。在上传两个文件的过程中,并非所有数据都得到正确处理,有时在结果开始之前就不会插入报告。
//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功能
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;
}