Return 数据来自 reader.readAsArrayBuffer
Return data from reader.readAsArrayBuffer
我有以下代码。我正在尝试 return 将 uploadData 数组返回到 hello 变量。但是,它 returning 作为一个空白数组。
控制台日志打印出正确的数据数组,如下所示:
{name: 'bob'},但是在 reader.readAsArrayBuffer 行之后,它变成了空白。有谁知道为什么?
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;
}
};
uploadData
在您的 reader.onload
函数被调用(异步调用)之前不会更新。因此,为了解决这个问题,您可以将回调函数传递给 validateSpreadsheet
以接收正确的 uploadData
.
validateSpreadsheet(file, rows, function (uploadData) {
// do something with the data here
console.log(uploadData);
});
validateSpreadsheet = function (fileUpload, templateRows, callback) {
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);
callback(uploadData); // the correct `uploadData` will be passed to your callback here
};
reader.readAsArrayBuffer(file);
return uploadData;
}
};
我有以下代码。我正在尝试 return 将 uploadData 数组返回到 hello 变量。但是,它 returning 作为一个空白数组。 控制台日志打印出正确的数据数组,如下所示: {name: 'bob'},但是在 reader.readAsArrayBuffer 行之后,它变成了空白。有谁知道为什么?
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;
}
};
uploadData
在您的 reader.onload
函数被调用(异步调用)之前不会更新。因此,为了解决这个问题,您可以将回调函数传递给 validateSpreadsheet
以接收正确的 uploadData
.
validateSpreadsheet(file, rows, function (uploadData) {
// do something with the data here
console.log(uploadData);
});
validateSpreadsheet = function (fileUpload, templateRows, callback) {
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);
callback(uploadData); // the correct `uploadData` will be passed to your callback here
};
reader.readAsArrayBuffer(file);
return uploadData;
}
};