API网关“{“消息”:“内部服务器错误”}“

问题描述 投票:0回答:0

在 API 网关中部署我的 API 后尝试使用调用 URL 时,我收到“内部服务器错误”(500 和 403)消息。在对我的 TableScan Lambda 函数进行一些修改后,我开始收到此错误(按照我收到的指示),我注意到这些错误似乎只出现在我的问题和答案资源的 GET 方法中,它利用了这个 TableScan Lambda 函数。

/**********************************************************************
 *  Scan table for all results matching filter
 **********************************************************************/

// we need access to the AWS SDK
var AWS = require("aws-sdk");

//  we need access to DynamoDB and choose the DocumentClient model
var docClient = new AWS.DynamoDB.DocumentClient();

const responseHeaders = {
  // HTTP headers to pass back to the client
  "Content-Type": "application/json",
  // the next headers support CORS
  "X-Requested-With": "*",
  "Access-Control-Allow-Headers":
    "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,x-requested-with",
  "Access-Control-Allow-Origin": "*",
  "Access-Control-Allow-Methods": "OPTIONS,*",
  // for proxies
  Vary: "Origin",
  // the "has-cors" library used by the Angular application wants this set
  "Access-Control-Allow-Credentials": "true",
};

async function queryDatabase(tableName, includeClause, whereClause) {
  // create the query params
  const paramQuery = async () => {
    // define our query
    let params = {
      TableName: tableName,
    };

    if (whereClause) {
      // get the key and value
      let whereKey = Object.keys(whereClause)[0];
      let whereValue = whereClause[whereKey];

      //  set our values
      params.ExpressionAttributeNames = { "#whereKey": whereKey };
      params.ExpressionAttributeValues = { ":whereValue": whereValue };
      params.FilterExpression = "#whereKey = :whereValue";
    }

    // log the params sent for the query
    //console.log(params);

    //  run the query and get the promise
    return new Promise((resolve, reject) => {
      var queryParams = docClient.scan(params).promise();
      queryParams
        .then(function (data) {
          resolve(data.Items);
        })
        .catch(function (err) {
          reject(err);
        });
    });
  };

  // wait for the promise and return the result
  return await paramQuery();
}

exports.handler = async (event) => {
  // lets get the resource
  let tableName = event.resource.slice(1, -1);

  // lets get the query parameters
  let query = event.queryStringParameters;
  var whereClause = "";
  var includeClause = "";

  if (query && query.filter && query.filter.trim()) {
    // we need some filter information
    let filter = JSON.parse(query.filter);

    if (filter && filter.where) {
      whereClause = filter.where;
    }

    if (filter && filter.include) {
      includeClause = filter.include;
    }
  }

  var data = await queryDatabase(tableName, includeClause, whereClause);

  // lets handle the include relations case.
  // we do it here instead of inside of queryDatabase to avoid recursion
  if (data && includeClause) {
    // need to query an nested includes
    // we are cheating a bit here, since we know that for this application
    // the only include is for Question to include the Answer
    // we need to do this because DynamoDB does not have support for Joins

    // get the requested relation
    let relation = includeClause.relation;

    // make sure the relation is not null and is answers
    if (relation && relation == "answers") {
      // need to get answers for each data elements
      for (const item of data) {
        let answerWhere = { questionId: item.id };
        const subData = await queryDatabase("Answer", null, answerWhere);
        if (subData) {
          item.answers = subData;
        }
      }
    }
  }

  let response = {
    statusCode: 200,
    body: JSON.stringify(data),
    // HTTP headers to pass back to the client
    headers: responseHeaders,
  };
  return response;
};

有人知道为什么会这样吗?

aws-lambda lambda amazon-dynamodb aws-api-gateway api-gateway
© www.soinside.com 2019 - 2024. All rights reserved.