title: Firebase: Function returned undefined, expected Promise or value
body: <p>I am writing a Firebase function where I am following the code from the sample code in <a href="https://github.com/firebase/functions-samples/blob/master/quickstarts/thumbnails/functions/index.js" rel="nofollow noreferrer">Github provided by Firebase</a>
  However, I am consistently getting the error 
  <code>Function returned undefined, expected Promise or value</code> in my Firebase functions log.</p>
  
  <p>I have pretty much modified my code to be exactly the same and yet no respite. Has anyone tried this code? Is it error free? Why am I getting the error? Same code is also in the <a href="https://firebase.google.com/docs/storage/extend-with-functions" rel="nofollow noreferrer">Firebase guide</a> </p>
  
  <p>Sample code producing the error is below</p>
  
  <pre><code>  exports.imageToJPG = functions.storage.object().onChange(event =&gt; {
    const object = event.data;
    const filePath = object.name;
    const baseFileName = path.basename(filePath, path.extname(filePath));
    const fileDir = path.dirname(filePath);
    const JPEGFilePath = path.normalize(path.format({dir: fileDir, name: baseFileName, ext: JPEG_EXTENSION}));
    const tempLocalFile = path.join(os.tmpdir(), filePath);
    const tempLocalDir = path.dirname(tempLocalFile);
    const tempLocalJPEGFile = path.join(os.tmpdir(), JPEGFilePath);
  
    // Exit if this is triggered on a file that is not an image.
    if (!object.contentType.startsWith('image/')) {
      console.log('This is not an image.');
      return;
    }
  
    // Exit if the image is already a JPEG.
    if (object.contentType.startsWith('image/jpeg')) {
      console.log('Already a JPEG.');
      return;
    }
  
    // Exit if this is a move or deletion event.
    if (object.resourceState === 'not_exists') {
      console.log('This is a deletion event.');
      return;
    }
  
    const bucket = gcs.bucket(object.bucket);
    // Create the temp directory where the storage file will be downloaded.
    return mkdirp(tempLocalDir).then(() =&gt; {
      // Download file from bucket.
      return bucket.file(filePath).download({destination: tempLocalFile});
    }).then(() =&gt; {
      console.log('The file has been downloaded to',
          tempLocalFile);
      // Convert the image to JPEG using ImageMagick.
      return spawn('convert', [tempLocalFile, tempLocalJPEGFile]);
  
  
  }).then(() =&gt; {
      console.log('JPEG image created at', tempLocalJPEGFile);
      // Uploading the JPEG image.
      return bucket.upload(tempLocalJPEGFile, {destination: JPEGFilePath});
    }).then(() =&gt; {
      console.log('JPEG image uploaded to Storage at', JPEGFilePath);
      // Once the image has been converted delete the local files to free up disk space.
      fs.unlinkSync(tempLocalJPEGFile);
      fs.unlinkSync(tempLocalFile);
    });
  });
  </code></pre>
  
  <p>Any pointers?</p>