我正在开发 Node.js / Express 项目,其中包括获取 Excel 文件的路径。我一直在使用 excel4node xlsx 生成库,它在本地机器上运行应用程序时效果很好。
但是在 Heroku 上使用该应用程序时,我收到错误 400,但没有任何有用信息。
以前我在上传图片时遇到过同样的问题,Amazon S3 解决了这些问题。我的结论是,这也与 Heroku 的“临时文件系统”有关,但作为初学者的 Web 开发人员,我很难掌握这一点。
路线代码如下。这仅适用于本地机器:
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();
});
});
我应该如何操作代码,以便将生成的 excel 文件下载到用户的默认下载文件夹?我不想将文件保存在云存储中的任何位置。
谢谢!
更新:错误代码
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)