AWS Lambda:“无法读取未定义的属性'0'”

时间:2020-11-10 03:36:21

标签: node.js amazon-web-services aws-lambda amazon-dynamodb

这是我的AWS Lambda函数。但是,运行它时,我得到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: ['keshijemi478@gmail.com'],
        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);
    });
};

1 个答案:

答案 0 :(得分:0)

您的代码似乎是一个AWS Lambda函数。

调用Lambda函数时,信息将通过event参数传递给该函数。通过event传递的信息取决于触发该函数的方式。例如,如果该功能由Amazon S3事件触发,则S3在event参数中提供信息,该信息描述导致事件被触发的对象。

但是,如果您“手动”触发此事件,则不涉及Amazon S3,并且event参数仅包含您在 调用函数时提供的信息。 / p>

如果要在AWS Lambda管理控制台中测试功能,则可以通过“配置测试”选项提供event。然后,此配置中提供的事件将被传递给正在测试的功能。