首页 > 解决方案 > onCall firebase 函数出现“超出最大调用堆栈大小”错误

问题描述

我用这个onCall方法做了一个firebase云功能。该函数获取上传的文件并调整其大小。

我只希望函数在调整大小和上传完成后返回一些东西,这样我就知道什么时候可以得到downloadURL. 该功能有效,它会调整大小并上传图像。问题是它仍然返回错误

“未处理的错误 RangeError:超过最大调用堆栈大小”在 firebase 函数日志中。

Firebase 功能:

exports.resizeImage = functions.https.onCall((data, context) => {
   const bucket = 'ezey-aa7b4.appspot.com';
   const contentType = data.contentType;
   const filePath = data.filePath;
   const fileName = filePath.split('/').pop();
   const dir = path.dirname(filePath);
   console.log("File change detected, function execution started");
   console.log(data);

   const destBucket = gcs.bucket(bucket);
   const tmpFilePath = path.join(os.tmpdir(), path.basename(filePath));
   const thumbName = path.join(os.tmpdir(), 'resized-' + fileName);
   const metadata = { contentType: contentType };

   return destBucket.file(filePath).download({
      destination: tmpFilePath
   }).then(() => {
      return sharp(tmpFilePath).resize(500, 500).toFile(thumbName, (err, info) => {
         if(err)
         {
            console.log(err)
         }
         console.log(info)
         destBucket.upload(thumbName, {
            destination: dir + '/resized-' + fileName,
            metadata: metadata
         }).then(() => {
            return {
               uploaded: true
            }
         }).catch((err) => {
            return {
               uploaded: false,
               error: err
            }
         });
      });
   }).catch((err) => {
      console.log(err)
   });
});

我的申请代码:

   var resizeImage = fb.functions().httpsCallable('resizeImage');

      resizeImage({filePath: file.filePath, contentType: file.contentType}).then(function(result){
         console.log(result)
      }).catch(function(error){
         console.log(error)
      });

给定的错误:

Unhandled error RangeError: Maximum call stack size exceeded
    at Function.map (/srv/node_modules/lodash/lodash.js:9556:31)
    at encode (/srv/node_modules/firebase-functions/lib/providers/https.js:226:18)
    at /srv/node_modules/lodash/lodash.js:13402:38
    at /srv/node_modules/lodash/lodash.js:4911:15
    at baseForOwn (/srv/node_modules/lodash/lodash.js:2996:24)
    at Function.mapValues (/srv/node_modules/lodash/lodash.js:13401:7)
    at encode (/srv/node_modules/firebase-functions/lib/providers/https.js:232:18)
    at /srv/node_modules/lodash/lodash.js:13402:38
    at /srv/node_modules/lodash/lodash.js:4911:15
    at baseForOwn (/srv/node_modules/lodash/lodash.js:2996:24)

标签: javascriptfirebasevue.jsgoogle-cloud-functions

解决方案


没关系,我修好了!我没有返回调整大小本身,只返回存储桶上传。

这是我的代码:

return destBucket.file(filePath).download({
      destination: tmpFilePath
   }).then(() => {
      sharp(tmpFilePath).resize(500, 500).toFile(thumbName);

      return destBucket.upload(thumbName, {
         destination: dir + '/resized-' + fileName,
         metadata: metadata
      }).then(() => {
         return {
            uploaded: true
         }
      });

   }).catch((err) => {
      console.log(err)
   });

代替:

return destBucket.file(filePath).download({
      destination: tmpFilePath
   }).then(() => {
      return sharp(tmpFilePath).resize(500, 500).toFile(thumbName, (err, info) => {
         if(err)
         {
            console.log(err)
         }

         console.log(info)

         return destBucket.upload(thumbName, {
            destination: dir + '/resized-' + fileName,
            metadata: metadata
         }).then(() => {
            return {
               uploaded: true
            }
         });

      });
   }).catch((err) => {
      console.log(err)
   });

推荐阅读