1

I am uploading images using express-fileupload following is my code to upload file it is in imageUpload.js file.

exports.driverImages  = function (req, imageFirstName) {
 if (!req) {
 console.log('no req')
 return new Promise(resolve => setTimeout( () => resolve(false), 
 1000));
 } else {
  let id = new mongoose.Types.ObjectId(imageFirstName);
  console.log(id);
  Driver.findById(id)
    .exec(function (err, result) {
      if (err) {
        console.log('err')
        return new Promise(resolve => setTimeout( () => resolve(false), 1000));

      } else {
        if (result !== null) {
          return new Promise(resolve => setTimeout( () => resolve(true), 1000));
        } else {
          console.log('no driver');
          return false;

        }
      }
    });
}
};

i am calling this function from driverController.js as following,

async function imageAsync(files, id) {
let data =  await imageUpload.driverImages(files, id); 
console.log('imgAsy') 
return data;
}

My problem is although i have return values at driverImages() it always returns undefined as return value. Please Help!.

Chathura
  • 33
  • 4
  • If you hit the `else` branch of the function, you have no return statement. The promises you are trying to await are created *in the callback* and then returned to nowhere useful. – Quentin Dec 06 '18 at 10:55
  • Thank you for commenting, i want to return true value to calling function if result !== null how can i achieve it ? – Chathura Dec 06 '18 at 11:11

0 Answers0