【问题标题】:Error: read ECONNRESET when resizing image with Firebase Cloud Functions错误:使用 Firebase Cloud Functions 调整图像大小时读取 ECONNRESET
【发布时间】:2017-08-14 11:49:21
【问题描述】:

我想使用 Firebase 函数调整存储在 Firebase 存储中的图像的大小。

基于 Firebase 团队提供的这个示例:https://github.com/firebase/functions-samples/blob/master/quickstarts/thumbnails/functions/index.js 我尝试编写一个由数据库事件触发的函数。

这是代码中最有趣的部分:

exports.myFunction = functions.database.ref('...').onWrite(event => {

        ...

        // Create thumbnails
        createThumbnail(1, ...);
        createThumbnail(2, ...);
        createThumbnail(3, ...);

         ...

        return; // <- Is this necessary ?
});

function createThumbnail(...) {

    ...

    return bucket
        .file(originalFilepath)
        .download({
            destination: tempFilePath
        })
        .then(() => {

            console.log('OK');

            ...

            // Generate a thumbnail using ImageMagick.
            return spawn('convert', [tempFilePath, '-thumbnail', dimension + 'x' + dimension + '>', tempFilePath])
                .then(() => {

                    ....

                    // Uploading the thumbnail.
                    return bucket.upload(tempFilePath, {
                            destination: thumbnailUrl
                        })
                        .then(() => {

                              ...

                            // Save thumbnailUrl in database
                            return admin.database().ref(...).set(thumbnailUrl);
                        });
                });
        });
}

在我看来一切都很好。但是代码永远不会转到console.log('OK');,我收到此错误:

Error: read ECONNRESET
    at exports._errnoException (util.js:1026:11)
    at TCP.onread (net.js:569:26)

有人知道可能是什么错误吗?

谢谢

【问题讨论】:

    标签: node.js firebase firebase-storage


    【解决方案1】:

    问题是您正在返回一个 Promise,该 Promise 在您的所有异步作业完成之前完成。

    当你这样做时:

    createThumbnail(1, ...);
    createThumbnail(2, ...);
    createThumbnail(3, ...);
    ...
    return;
    

    您正在启动 3 个异步 createThumbnail 任务,但您将立即返回。因此,Cloud Functions 实例被关闭,您的 3 createThumbnail 没有时间完成,这时您收到 ECONNRESET 错误。

    每个createThumbnail 返回一个Promise。您需要做的是使用 Promise.all 返回一个 Promise,它在 3 个 createThumbnail Promise 完成时完成:

    const listOfAsyncJobs = [];
    listOfAsyncJobs.push(createThumbnail(1, ...));
    listOfAsyncJobs.push(createThumbnail(2, ...));
    listOfAsyncJobs.push(createThumbnail(3, ...));
    ...
    return Promise.all(listOfAsyncJobs); // This will ensure we wait for the end of the three aync tasks above.
    

    【讨论】:

      猜你喜欢
      • 2020-03-20
      • 1970-01-01
      • 1970-01-01
      • 2018-09-09
      • 2020-05-10
      • 2018-06-11
      • 2013-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多