Error: read ECONNRESET when resizing images using Firebase cloud functions

I would like to resize the image stored in Firebase Storage using the Firebase features.

Based on this example provided by the Firebase team: https://github.com/firebase/functions-samples/blob/master/quickstarts/thumbnails/functions/index.js I tried to write a function called by a database event.

Here is the most interesting part of the code:

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

Everything looks good to me. However, the code never goes into console.log('OK');, and I get this error:

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

Does anyone know what could be a mistake?

thank

+4
source share
1 answer

, , .

:

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

3 createThumbnail, . , 3 createThumbnail , ECONNRESET.

createThumbnail Promise. , Promise.all Promise, , 3 createThumbnail Promises:

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.
+3

Source: https://habr.com/ru/post/1672913/


All Articles