javascript - 如何调用 Firebase Cloud 函数中的两个函数

标签 javascript firebase google-cloud-firestore google-cloud-functions

我使用一个 Cloud Function 来调整图像大小,使用第二个 Cloud Function 将新图像 URL 上传到 Cloud Firestore。

但是有些东西不起作用,因为第二个函数永远不会运行。 我需要 uid 和 postId 来更新 url。

如何调用第二个函数来更新 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),
            });
        });

        await Promise.all(uploadPromises);

      //second function

        functions.firestore.document('users/{uid}/posts/{id}').onCreate(async (snap, context) => {
            console.log(context.params);
            const uid = context.params.uid;

            const userPost = functions.firestore.doc('users/{uid}/posts}');
            userPost.update({
                url: newUrl,
            });
        });

        return fse.remove(workingDir);
    });

最佳答案

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

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

关于javascript - 如何调用 Firebase Cloud 函数中的两个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57360870/

相关文章:

android - Firebase Analytics 如何定义 "user"(为了受众的目的)?

firebase - Flutter firestore 复合查询

java - Firestore 根据条件执行删除

javascript - Jquery 的新手并尝试从 Prototype 翻译一些代码

javascript - Angular 自定义验证指令 - 如何正确跳过 ng-disabled 字段?

javascript - 如何使用c# asp.net上传默认文件

android - Firebase 消息 “E/FirebaseCrash: Failed to initialize crash reporting”

javascript - Firebase 数据库 - 仅从程序写入数据,而不是人员

android - 我应该将我的 firebase 数据库迁移到 firestore 数据库吗?

javascript - 为什么我不能在 VueJS 的 for 循环中使用变量?