Jemi
Jemi

Reputation: 11

AWS Lambda: "Cannot read property '0' of undefined"

Here is my AWS Lambda function. However, when running it, I get Cannot read property '0' of undefined.

const AWS = require('aws-sdk');
const SES = new AWS.SES();

const FROM_EMAIL_ADDRESS = process.env.FROM_EMAIL_ADDRESS;
const TO_EMAIL_ADDRESS = process.env.TO_EMAIL_ADDRESS;

function sendEmailToMe(formData) {

    const emailParams = {
        Source: FROM_EMAIL_ADDRESS, 
        ReplyToAddresses: ['[email protected]'],
        Destination: {
          ToAddresses: [TO_EMAIL_ADDRESS], 
        },
        Message: {
          Body: {
            Text: {
              Charset: 'UTF-8',
              Data: `Thanks for subscribe: ${formData.message}\n\n Name: ${formData.name}\n Email: ${formData.email}\n I will reply as soon as possible`,
            },
          },
          Subject: {
            Charset: 'UTF-8',
            Data: 'New message from your_site.com',
          },
        },
    };

    console.log(emailParams);

    const promise =  SES.sendEmail(emailParams).promise();
    console.log(promise);
    return promise;
}


exports.handler = async(event) => {
    console.log('SendEmail called');

  
   const dynamodb = event.Records[0].dynamodb;
    console.log(dynamodb);

    const formData = {
        name : dynamodb.NewImage.name.S,
        message : dynamodb.NewImage.message.S,
        email : dynamodb.NewImage.email.S
    };
    console.log(formData);

    return sendEmailToMe(formData).then(data => {
        console.log(data);
    }).catch(error => {
        console.log(error);
    });
};

Upvotes: 1

Views: 6091

Answers (1)

John Rotenstein
John Rotenstein

Reputation: 269111

It appears that your code is an AWS Lambda function.

When a Lambda function is called, information is passed to the function via the event parameter. The information passed via event varies depending upon how the function was triggered. For example, if the function is triggered by an Amazon S3 Event, then S3 provides information in the event parameter that describes the object that caused the event to be triggered.

If, however, you trigger this event 'manually', then Amazon S3 is not involved and the event parameter only contains the information that you provided when you invoked the function.

If you are testing the function in the AWS Lambda management console, you can supply an event via the "Configure Test" option. The event provided in this configuration will then be passed to the function being tested.

Upvotes: 2

Related Questions