"Cannot read property 'push' of undefined" when chaining S3 actions with promises

Viewed 1799

I can't figure out this one. Any help appreciated.

I have a lambda that basically copies HTML files from one S3 bucket to another S3 bucket including some "magic" (reusing parts of other HTML files).

I had this up and running inside a nodes.js 10

exports.handler = async (event, context, callback) => { }

with callbacks. Switching to a node.js 12 with

exports.handler = async (event) => { }

I had to switch from callbacks to promises.

The resulting code works in 60% of the cases.

exports.handler = async (event) => {
for (var record of event.Records) {
    if(record.s3.object.key.indexOf(".") != -1){
        if (record.s3.object.key.localeCompare("website/") != 0) {
            const objectCopied = await s3.putObject( { Body: (await generateHTML( (await s3.getObject({ Bucket: record.s3.bucket.name, Key: record.s3.object.key }).promise()).Body.toString('utf-8'), record.s3.bucket.name )), Bucket: DestBucket, Key: record.s3.object.key.slice("website/".length), ACL: 'public-read',  ContentType: 'text/html'}).promise();
        }           
    }
}    
return 0;
};

In the other 40%, I get this error:

Response:
{
  "errorType": "TypeError",
  "errorMessage": "Cannot read property 'push' of undefined",
  "trace": [
    "TypeError: Cannot read property 'push' of undefined",
    "    at Request.HTTP_DATA (/var/runtime/node_modules/aws-sdk/lib/event_listeners.js:389:35)",
    "    at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:106:20)",
    "    at Request.emit (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:78:10)",
    "    at Request.emit (/var/runtime/node_modules/aws-sdk/lib/request.js:683:14)",
    "    at IncomingMessage.onReadable (/var/runtime/node_modules/aws-sdk/lib/event_listeners.js:289:32)",
    "    at IncomingMessage.emit (events.js:311:20)",
    "    at IncomingMessage.EventEmitter.emit (domain.js:482:12)",
    "    at emitReadable_ (_stream_readable.js:562:12)",
    "    at onEofChunk (_stream_readable.js:540:5)",
    "    at readableAddChunk (_stream_readable.js:242:5)"
  ]
}

I have no idea, what I'm doing wrong here. Please help!

One more sentence about the "why": My research brought up, that AWS had problems with chaining promises, but the suggested solution I found was using callbacks. This, as far as I know, is not an option with nodejs 12 plus it looks super ugly in the code.

0 Answers
Related