Skip to content
Advertisement

Return statement of a function doesn’t get triggered after using it in Express FileUpload .mv() method

I’m trying to upload a file through Express File Upload but getting an undefined value of the function in which it is being called.

I have this function which checks if the user chose a single file or multiple files. To keep it simple I am just going to show the issue in single file upload. That is req.files.fileInput is an object instead of an array of objects.

Here’s the code:

JavaScript

I get this error:

JavaScript

Seems like fileCheck function’s return statement isn’t getting triggered after using it in .mv() method. What could be the reason here? And what can I do to solve it?

Advertisement

Answer

You’re only returning from the file.mv() callback function, you need to return file.mv() as well, such that it percolates up to your fileCheck function.

Do this

JavaScript

EDIT

Unfortunately the file.mv() doesn’t seem to return the result of its callback function by default.

Consider returning a promise from fileCheck like this

JavaScript

And using fileCheck like this

JavaScript

NB: You have to make the parent function where fileCheck is being called async in other to use await

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement