I had working express Node Javascript code, but when I changed the code to give me a random id for each file input (instead of a set file name, myFile.jpeg), I get the following error:
UnhandledPromiseRejectionWarning: ReferenceError: fileName is not defined
I know that I've defined the fileName, but I don't understand what this error means or how to fix it. I've seen this error a lot of Discord bots or MongoDB, but I'm having a hard time finding anything for express.
let rid = Math.floor(Math.random() * 10000000)
let ridString = "" + rid
//const upload = multer().single('myfile')
const upload = multer().single(ridString)
//other code
app.post('/upload', (req, res)=>{
upload(req, res, async function(err){
if( err|| req.file === undefined){
console.log(err)
res.send("error occured")
}else{
//let rid = Math.floor(Math.random() * 10000000)
//let ridString = "" + rid
let fileName = ridString + ".jpeg"
//let fileName = "myfile.jpeg"
var image = await sharp(req.file.buffer)
//.catch(err => {console.log('error: ', err)})
.jpeg({
//quality: 40,
}).toFile('./uploads/'+fileName)
.catch( err => { console.log('error: ', err) })
pythonFunction(res, fileName);
}
})
})
I've commented out everything that got changed from myFile to the random number.