'ERR Error: End of data reached (data length = 0, asked index = 4). Corrupted zip?
im making a whatsapp bot, im saving the chats in a excel file (im using exceljs) to read and handle some responses, i prefer to use promises and resolve them, i have a function to read chats:
const readChat = (number, message, step = null) =>
new Promise((resolve, reject) => {
setTimeout(() => {
number = number.replace("@c.us", "");
number = `${number}@c.us`;
const pathExcel = `./chats/${number}.xlsx`;
const workbook = new ExcelJS.Workbook();
const today = moment().format("DD-MM-YYYY hh:mm");
if (fs.existsSync(pathExcel)) {
const workbook = new ExcelJS.Workbook();
workbook.xlsx
.readFile(pathExcel)
.then(() => {
const worksheet = workbook.getWorksheet(1);
const lastRow = worksheet.lastRow;
let getRowInsert = worksheet.getRow(++lastRow.number);
getRowInsert.getCell("A").value = today;
getRowInsert.getCell("B").value = message;
if (step) {
getRowInsert.getCell("C").value = step;
}
getRowInsert.commit();
workbook.xlsx
.writeFile(pathExcel)
.then(() => {
const getRowPrevStep = worksheet.getRow(lastRow.number);
const lastStep = getRowPrevStep.getCell("C").value;
resolve(lastStep);
})
.catch((err) => {
console.log("ERR", err);
reject("error");
});
})
.catch((err) => {
console.log("ERR", err);
reject("error");
});
} else {
const worksheet = workbook.addWorksheet("Chats");
worksheet.columns = [
{ header: "Fecha", key: "number_customer" },
{ header: "Mensajes", key: "message" },
{ header: "Paso", key: "step" },
];
step = step || "";
worksheet.addRow([today, message, step]);
workbook.xlsx
.writeFile(pathExcel)
.then(() => {
resolve("STEP_1");
})
.catch((err) => {
console.log("Error", err);
reject("error");
});
}
}, 1000);
});
but the code after that else, its giving me the error: ERR Error: End of data reached (data length = 0, asked index = 4). Corrupted zip ?, the else, is supposed to be executed when there is no excel saved with that chat. I searched in many questions and pages, but i have not found any solution
Solution 1:[1]
I believe you would get this error after you stop your execution in between, i.e when the file is getting updated and this would corrupt the excel file.
You could try adding a "try catch" around read file in your if condition and create a new copy of workbook whenever you get these particular errors ('End of data reached (data length = 0, asked index = 4). Corrupted zip ?' or 'File not found:')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Vivek Pandey |
