3

I am writing a Firebase function where I am following the code from the sample code in Github provided by Firebase However, I am consistently getting the error Function returned undefined, expected Promise or value in my Firebase functions log.

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 Firebase guide

Sample code producing the error is below

  exports.imageToJPG = functions.storage.object().onChange(event => {
  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(() => {
    // Download file from bucket.
    return bucket.file(filePath).download({destination: tempLocalFile});
  }).then(() => {
    console.log('The file has been downloaded to',
        tempLocalFile);
    // Convert the image to JPEG using ImageMagick.
    return spawn('convert', [tempLocalFile, tempLocalJPEGFile]);


}).then(() => {
    console.log('JPEG image created at', tempLocalJPEGFile);
    // Uploading the JPEG image.
    return bucket.upload(tempLocalJPEGFile, {destination: JPEGFilePath});
  }).then(() => {
    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);
  });
});

Any pointers?

Abhishek
  • 1,261
  • 1
  • 13
  • 30
  • "Questions seeking debugging help ('why isn't this code working?') must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve)." – Frank van Puffelen Dec 24 '17 at 16:50
  • The sample code and documentation have not yet been updated to reflect a recent change in the SDK. See this question: https://stackoverflow.com/q/47128440/4815718, and in particular, Frank's comment there. – Bob Snyder Dec 24 '17 at 21:50
  • @BobSnyder - thanks Bob, your comment has been extremely helpful. I have managed to find the answer to the question. And you were bang on. Appreciate the pointer – Abhishek Dec 25 '17 at 03:15

1 Answers1

12

It seems that recently Firebase has updated their SDK due to which their sample code and documentation is little out of date. You must return with a boolean value even if you are just trying to exit the function. So it must be return true for each of the return statements in the code above where there is no Promise being returned.

I will delete this question and answer once Firebase has updated their sample code and documentation. Till then leaving it here for those who may still stumble upon this issue without knowing why.

Abhishek
  • 1,261
  • 1
  • 13
  • 30
  • Not sure where you have to return true !? In your case you have `return bucket.upload(tempLocalJPEGFile, {destination: JPEGFilePath});` how can you return true!? – Famic Tech Mar 25 '18 at 01:43
  • 1
    @FamicTech - In the first 3 return statements, those after the comments // Exit if this is triggered on a file that is not an image. // Exit if the image is already a JPEG and // Exit if this is a move or deletion event – Abhishek Mar 25 '18 at 16:03