I'm working on Node.js / Express project which includes route to get Excel file. I have been using excel4node xlsx generation library and it works perfectly when running app on local machine.
But when the app is used on Heroku, I receive error 400 without any useful information.
Previously I encountered same kind of issues with image upload and Amazon S3 solved these issues. My conclusion is that also this has something to do with "ephemeral filesystem" of Heroku but as a beginner web developer, I have a hard time to get grasp of that.
The route code is below. This works on local machine only:
router.get("/excel", authenticate, (req, res) => {
Product.find({quantity: { $gt: 0 }}).sort({ code: 1 }).then((products, e) => {
const date = moment();
const time = date.format("DD.MM.YYYY");
const sheetName = `Stock ${time}`
const workbookName = `Company XYZ stock ${time}.xlsx`
const workbook = new excel.Workbook()
const worksheet = workbook.addWorksheet(sheetName);
const style = workbook.createStyle({
font: {
bold: true,
size: 12
}
});
worksheet.cell(1, 1).string("Code").style(style);
worksheet.cell(1, 2).string("Description").style(style);
worksheet.cell(1, 3).string("Quantity").style(style);
worksheet.cell(1, 4).string("Class").style(style);
worksheet.cell(1, 5).string("Retail price").style(style);
worksheet.cell(1, 6).string("Net price").style(style);
products.forEach((product) => {
const cell = products.indexOf(product) + 2
worksheet.cell(cell, 1).string(product.code)
worksheet.cell(cell, 2).string(product.description)
worksheet.cell(cell, 3).number(product.quantity)
worksheet.cell(cell, 4).string(product.class)
worksheet.cell(cell, 5).number(product.price)
worksheet.cell(cell, 6).number(product.netprice)
})
workbook.write(workbookName, res);
}).catch((e) => {
res.status(400).send();
});
});
How should I manipulate the code so generated excel file would be downloaded to users default download folder? I don't want to save the file anywhere in cloud storage.
Thanks!
UPDATE: ERROR CODE
2019-07-23T04:36:24.256616+00:00 app[web.1]: TypeError: Value sent to Number function of cells ["E181"] was not a number, it has type of object and value of null
2019-07-23T04:36:24.256629+00:00 app[web.1]: at cellBlock.numberSetter [as number] (/app/node_modules/excel4node/distribution/lib/cell/index.js:77:15)
2019-07-23T04:36:24.256632+00:00 app[web.1]: at products.forEach (/app/routes/products.js:117:37)
2019-07-23T04:36:24.256634+00:00 app[web.1]: at Array.forEach (<anonymous>)
2019-07-23T04:36:24.256636+00:00 app[web.1]: at Product.find.sort.then (/app/routes/products.js:109:18)
2019-07-23T04:36:24.256638+00:00 app[web.1]: at process._tickCallback (internal/process/next_tick.js:68:7)