I'm facing an issue with the following code. My goal is to return the uploadData array back to the hello variable, but it's returning as a blank array. Even though the console log shows the correct array of data like {name: 'bob'}, it becomes blank after the reader.readAsArrayBuffer line. Any insights on why this might be happening?
Hello = validateSpreadsheet(file, rows)
validateSpreadsheet = function (fileUpload, templateRows) {
let uploadData = [];
for (const file of fileUpload.files) {
const fileExtension = file.name.split(".").pop();
let valid = false;
if (fileExtension === "csv") {
valid = true;
}
if (!valid) {
throw "Unsupported file type, file must be 'of type .csv";
}
const reader = new FileReader();
reader.onload = async (e) => {
const data = new Uint8Array(e.target.result);
const workbook = XLSX.read(data, {
type: "array",
cellDates: true,
dateNF: "dd/mm/yyyy",
});
if (workbook.SheetNames.length === 0) {
console.error("No sheets");
}
for (const sheetName of workbook.SheetNames) {
const rows = XLSX.utils.sheet_to_json(workbook.Sheets[sheetName], {
defval: null,
});
for (let row of rows) {
uploadData.push(row);
}
}
console.log(uploadData);
};
reader.readAsArrayBuffer(file);
return uploadData;
}
};