从网站执行时,Lambda NodeJS函数不会触发,但在控制台中可以正常运行

时间:2019-02-11 22:54:57

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

这是我第一次尝试使用NodeJS创建lambda函数。正确的做法是先发送电子邮件,然后再打开Contact_Success.html页面。

从我的网站通过API网关调用该函数时,它不会发送电子邮件,但是会打开Contact_Success.html页。

在AWS控制台中通过API Gateway测试或Lambda Dev控制台进行测试时,将发送电子邮件 do

代码如下:

const AWS = require('aws-sdk');
const querystring = require('querystring');
AWS.config.region = 'us-east-1';

exports.handler = function(evt, context, callback) {
    // Our raw request body will be in evt.body.
    const params = querystring.parse(evt.body);

    // Our field from the request.
    const first_name = params['first_name'];
    const last_name = params['last_name'];
    const emailaddr = params['email'];
    const telephone = params['telephone'];
    const comments = params['comments'];

    var eParams = {
        Destination: {
            ToAddresses: ["blahblah@blahblah.com"]
        },
        Message: {
            Body: {
                Text: {
                  //  Data: first_name 
                  Data: first_name + " " + last_name + " at " + emailaddr + " phone:" + telephone + " post:" + comments
                }
            },
            Subject: {
                Data: "BlaBlah Inquirty"
            }
        },
        Source: ‘blahblah@blahblah.com'
    };


      // Create the promise and SES service object
  const sendPromise = new AWS.SES({ apiVersion: "2010-12-01" })
    .sendEmail(eParams)
    .promise();

  // Handle promise's fulfilled/rejected states
  sendPromise
    .then(data => {
      console.log(data.MessageId);
    // Generate HTML.
      const html = `<script type="text/javascript">window.location = "http://blahblah.com/Contact_Success.html"</script>`;
    // Return HTML as the result.
     callback(null, html);
     context.done(null, "Success");
    })
    .catch(err => {
      console.error(err, err.stack);
    // Generate HTML.
      const html = `<script type="text/javascript">window.location = "http://blahblah.com/Contact_Error.html"</script>`;
    // Return HTML as the result.
      callback(null, html);
      context.done(null, "Failed");
    });
};

1 个答案:

答案 0 :(得分:0)

我将通过查看cloudWatch中的日志来开始调试