Firebase:函数返回undefined,预期Promise或value

问题描述 投票:2回答:1

我正在写一个Firebase函数,我正在跟踪Github provided by Firebase中示例代码中的代码。但是,我一直在我的Firebase函数日志中收到错误Function returned undefined, expected Promise or value

我几乎修改了我的代码,但却没有任何喘息的机会。有人试过这段代码吗?它没有错误吗?为什么我收到错误?同样的代码也在Firebase guide

产生错误的示例代码如下

  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);
  });
});

有什么指针吗?

javascript firebase google-cloud-functions
1个回答
8
投票

最近Firebase似乎更新了他们的SDK,因为他们的示例代码和文档很少过时。即使您只是试图退出该函数,您也必须使用布尔值return。因此,对于上面代码中的每个return true,它必须是return statements,其中没有返回Promise。

一旦Firebase更新了示例代码和文档,我将删除此问题并回答。然后将其留在这里,以便那些可能在不知道原因的情况下偶然发现这个问题的人。

© www.soinside.com 2019 - 2024. All rights reserved.