Error handling on readFile
I am trying to automate an xls file download and import in Mongo process. Every so often I think my file download is not working properly and I get erorrs from Xlsx [TypeError: Cannot read property '3' of undefined], but I am unable to find a way to catch these errors with code - can you advise?
function xls2Json(fname) {
return new Promise( (resolve, reject) => {
var wbook = XLSX.readFile(fname+'.xls');
var worksheet = wbook.Sheets[SHEET_NAME];
let json = XLSX.utils.sheet_to_json(worksheet, {header:headers, range:1});
resolve(json);
});
}
You can try to use try ... catch
function xls2Json(fname) {
return new Promise( (resolve, reject) => {
try {
var wbook = XLSX.readFile(fname+'.xls');
} catch (err) {
console.log(err);
}
var worksheet = wbook.Sheets[SHEET_NAME];
let json = XLSX.utils.sheet_to_json(worksheet, {header:headers, range:1});
resolve(json);
});
}
@simonh1000 this bug is most likely coming from trying to read an HTML file. Lots of so-called Excel exporters really generate HTML and expect Excel to process it. We have since added a very rudimentary parser. Test out the latest version and let us know if it works.
If there are parse errors, the best approach is to catch errors and then decide how to handle the error.