I have a web app built using node and express that uses a python script to generate an excel file. The issue is that although node is successfully handling the request to create the excel file, the handler is also trying to download the file just created but is giving the error: Error: ENOENT: no such file or directory
. I'm assuming this is because it is trying to access the file before it is created but I thought that by using await
the file would be created first. Here is my code:
Routes:
router.post('/executepythonscript', db.executePythonScript)
Handler:
const executePythonScript = async (request, response) => {
const { spawn } = require('child_process');
let pythonPromise = new Promise((resolve, reject) => {
const python = spawn('py', ['./scripts/generate_excel.py', request.body.week]);
python.stdout.on("data", (data) => {
resolve(data.toString());
});
python.stderr.on("data", (data) => {
reject(data.toString());
});
python.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
});
try {
await pythonPromise;
} catch (error) {
console.log(error);
}
response.download('[myPath]/timetracking.xlsx');
}
Am I using await
wrong or why would the program try to open the file before it is created?