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

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


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

return file.mv(uploadPath, function (err) {  if (err) {    console.error(err);    return {      success: false,      message: 'Something went wrong. Please upload again!',      data: null,    };  }  return {    success: true,    message: 'File Uploaded Successfully!',    data: file.name,  };});

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

const fileCheck = ( file ) => {  if ( Array.isArray( file ) ) {     console.log( 'THE USER WANTS TO UPLOAD MULTIPLE FILES' );  } else if ( typeof file === 'object' ) {     console.log( 'THE USER WANTS TO UPLOAD A SINGLE FILE' );     const uploadPath = `./resources/images/${file.name}`;     return new Promise((resolve, reject) =>        file.mv( uploadPath, function ( err ) {         if ( err ) {             console.error( err );             return reject({                 success: false,                 message: 'Something went wrong. Please upload again!',                 data: null             });         }         return resolve({             success: true,             message: 'File Uploaded Successfully!',             data: file.name         });      });  }};

And using fileCheck like this

const { success, message, data } = await fileCheck(req.files.fileInput);console.log( success ); 

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