NodeJS lambda函数-无效响应-在完成请求之前退出了进程

时间:2019-03-20 03:15:42

标签: javascript node.js aws-lambda netlify

我有通过Netlify部署的NodeJS AWS lambda函数:

require('dotenv').config();
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
const statusCode = 200;
const headers = {
  "Access-Control-Allow-Origin" : "*",
  "Access-Control-Allow-Headers": "Content-Type"
};
exports.handler = function(event, context, callback) {
  //-- We only care to do anything if this is our POST request.
  if(event.httpMethod !== 'POST' || !event.body) {
    callback(null, {
      statusCode,
      headers,
      body: ''
    });
  }
  //-- Parse the body contents into an object.
  console.log(event);
  console.log(event.body);
  const data = JSON.parse(event.body);
  //-- Make sure we have all required data. Otherwise, escape.
  if(
    !data.token ||
    !data.amount ||
    !data.idempotency_key
  ) {
    console.error('Required information is missing.');
    callback(null, {
      statusCode,
      headers,
      body: JSON.stringify({status: 'missing-information'})
    });
    return;
  }
  stripe.charges.create(
    {
      currency: 'usd',
      amount: data.amount,
      source: data.token.id,
      receipt_email: data.token.email,
      description: `charge for a widget`
    },
    {
      idempotency_key: data.idempotency_key
    }, (err, charge) => {
      if(err !== null) {
        console.log(err);
      }
      let status = (charge === null || charge.status !== 'succeeded')
        ? 'failed'
        : charge.status;
      callback(null, {
        statusCode,
        headers,
        body: JSON.stringify({status})
      });
    }
  );
}

当我尝试处理付款时,说收费失败。我的控制台显示以下输出:

HTTP502: BAD GATEWAY - The server, while acting as a gateway or proxy, received an invalid response from the upstream server it accessed in attempting to fulfill the request.
GET - http://spiroartdemo.netlify.com/.netlify/functions/purchase/

这是该功能的链接: https://spiroartdemo.netlify.com/.netlify/functions/purchase

当我查看日志时,它说:

{"errorMessage":"RequestId: 0f83acda-87fa-4d37-92e7-2208f799fb33 Process exited before completing request"}

11:40:44 PM: purchase invoked
11:40:44 PM: SyntaxError: Unexpected end of JSON input    at JSON.parse (<anonymous>)    at t.handler (/var/task/purchase.js:1:15721)RequestId: 030c55fe-de2a-4630-9e25-c83ab186d01c Process exited before completing request

我不确定,但我想也许已经到了这一行:

const data = JSON.parse(event.body);

问题

即使发帖请求正文为空(直接访问文件),我的代码也不会以这种方式失败。谁能告诉我为什么文件甚至无法完成处理?

0 个答案:

没有答案