为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 Firebase Cloud 函数中调用两个函数

如何在 Firebase Cloud 函数中调用两个函数

慕标琳琳 2021-09-04 14:55:02
我使用一个 Cloud Function 来调整图像大小,第二个用于将新的图像 URL 上传到 Cloud Firestore。但是有些东西不起作用,因为第二个函数永远不会运行。我需要可以更新 url 的 uid 和 postId。如何调用第二个函数来更新 Firestore 中的 img url?代码const { functions, tmpdir, dirname, join, sharp, fse, gcs } = require('../../admin');const runtimeOpts = {    timeoutSeconds: 120,    memory: '1GB',};exports.resizeImages = functions    .runWith(runtimeOpts)    .storage.object()    .onFinalize(async (object, context) => {        const bucket = gcs.bucket(object.bucket);        const filePath = object.name;        const fileName = filePath.split('/').pop();        const bucketDir = dirname(filePath);        const workingDir = join(tmpdir(), 'resize');        const tmpFilePath = join(workingDir, 'source.png');        if (fileName.includes('@s_') || !object.contentType.includes('image')) {            return false;        }        await fse.ensureDir(workingDir);        await bucket.file(filePath).download({ destination: tmpFilePath });        // creates 3 new images with these sizes..        const sizes = [1920, 720, 100];        var newUrl = null;        const uploadPromises = sizes.map(async size => {            const ext = fileName.split('.').pop();            const imgName = fileName.replace(`.${ext}`, '');            const newImgName = `${imgName}@s_${size}.${ext}`;            var imgPath = join(workingDir, newImgName);            newUrl = imgPath;            await sharp(tmpFilePath)                .resize({ width: size })                .toFile(imgPath);            return bucket.upload(imgPath, {                destination: join(bucketDir, newImgName),            });        });
查看完整描述

1 回答

?
隔江千里

TA贡献1906条经验 获得超10个赞

您的第二个功能似乎嵌入在第一个功能中。这行不通。所有函数定义都必须在顶层,以便 Firebase CLI 可以检测到它们并单独部署。

如果您实际上不需要两个单独的函数定义,只需在一个函数中执行所有工作,不要尝试使用函数 SDK 来完成任何工作。Functions SDK 仅用于定义用于部署的函数。


查看完整回答
反对 回复 2021-09-04
  • 1 回答
  • 0 关注
  • 171 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信