简体   繁体   中英

Lambda is giving me Invoke Error - Cannot read property 'method' of undefined

I have this lambda function that I have created and for whatever reason it is not working. I have multiple other queries I have created in lambda connecting and updating Dynamo db. But this one is giving me an error. I have included the other parts of the code.

LAMBDA CODE

const https = require('https');
const util = require('util');

const ddb = new AWS.DynamoDB.DocumentClient();

exports.handler = async (event) => {
    //console.log('event: ', util.inspect(event, { depth: null }), "\n");

    if (event.requestContext.http.method == "POST") {
        var data = event.body;
        if (typeof(data) != "object")
            data = JSON.parse(data);

        var card = await getCardData(event.queryStringParameters.game_id, event.requestContext.http.sourceIp);
        if (card.Item !== undefined && card.Item.game_id !== undefined && card.Item.player_id !== undefined) {
            //console.log("card exists, updating card");
            var jindoData = JSON.parse(card.Item.jindoData);
     
            
            jindoData[0].jindoResult = data[0].jindoResult;
            jindoData[0].jindoTimeCalled = data[0].jindoTimeCalled;
            await callJindo(card,jindoData);
        }
        
    }

    const response = {
        statusCode: 200,
        body: JSON.stringify('Success.'),
    };
    return response;
};


// async function abstraction
async function getCardData(game_id, player_id) {
    var params = {
        TableName: 'carddata',
        Key: {
            'game_id': game_id,
            'player_id': player_id
        }
    };
    try {
        const data = await ddb.get(params).promise();
        return data;
    }
    catch (err) {
        return err;
    }
}

function callJindo(card,jindoData) {
    const params = {
        TableName: "carddata",
        Key: {
            "game_id": card.Item.game_id,
            "player_id": card.Item.player_id
        },
        UpdateExpression: "set #jindoData = :jindoData",
        ExpressionAttributeNames: {
            "#jindoData": "jindoData"
        },
        ExpressionAttributeValues: {
            ":jindoData": JSON.stringify(jindoData),
        }
    };
    return ddb.update(params).promise();
}

Essentially all this is supposed to be doing is grabbing the data and updating the record in dynamo

STRINGIFY JSON DATA POSTED TO LAMBDA VIA AJAX

[{"jindoResult":1,"jindoTimeCalled":1600377298099}]

Error that I am getting in lambda logs

2020-09-17T21:14:58.576Z    10d5550f-9d06-42bc-bddd-085d3f7539cb    ERROR   Invoke Error    
{
    "errorType": "TypeError",
    "errorMessage": "Cannot read property 'method' of undefined",
    "stack": [
        "TypeError: Cannot read property 'method' of undefined",
        "    at Runtime.exports.handler (/var/task/index.js:10:35)",
        "    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
    ]
}

Any help or Direction would be much appreciated I have been stuck on this problem for a couple of days and am at a halt.

UPDATED Here is the Event information coming through. EVENT

INFO    event:  {
  version: '1.0',
  resource: '/callJindo',
  path: '/callJindo',
  httpMethod: 'POST',
  headers: {
    'Content-Length': '51',
    'Content-Type': 'application/json',
    Host: 'api.jindo.live',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36',
    'X-Amzn-Trace-Id': 'Root=1-5f644727-8f9c025cfdd0741c2d6879ea',
    'X-Forwarded-For': '68.2.174.49',
    'X-Forwarded-Port': '443',
    'X-Forwarded-Proto': 'https',
    accept: 'application/json, text/javascript, */*; q=0.01',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'en-US,en;q=0.9',
    origin: 'REDACTED',
    referer: 'REDACTED',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-site'
  },
  multiValueHeaders: {
    'Content-Length': [ '51' ],
    'Content-Type': [ 'application/json' ],
    Host: [ 'api.jindo.live' ],
    'User-Agent': [
      'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36'
    ],
    'X-Amzn-Trace-Id': [ 'Root=1-5f644727-8f9c025cfdd0741c2d6879ea' ],
    'X-Forwarded-For': [ '68.2.174.49' ],
    'X-Forwarded-Port': [ '443' ],
    'X-Forwarded-Proto': [ 'https' ],
    accept: [ 'application/json, text/javascript, */*; q=0.01' ],
    'accept-encoding': [ 'gzip, deflate, br' ],
    'accept-language': [ 'en-US,en;q=0.9' ],
    origin: [ 'https://beta.jindo.live' ],
    referer: [ 'REDACTED' ],
    'sec-fetch-dest': [ 'empty' ],
    'sec-fetch-mode': [ 'cors' ],
    'sec-fetch-site': [ 'same-site' ]
  },
  queryStringParameters: { game_id: 'G1600389708547' },
  multiValueQueryStringParameters: { game_id: [ 'G1600389708547' ] },
  requestContext: {
    accountId: '304887485769',
    apiId: '6vf75oo0sc',
    domainName: 'REDACTED',
    domainPrefix: 'api',
    extendedRequestId: 'TDAOLjpLvHcEJsA=',
    httpMethod: 'POST',
    identity: {
      accessKey: null,
      accountId: null,
      caller: null,
      cognitoAmr: null,
      cognitoAuthenticationProvider: null,
      cognitoAuthenticationType: null,
      cognitoIdentityId: null,
      cognitoIdentityPoolId: null,
      principalOrgId: null,
      sourceIp: '68.2.174.49',
      user: null,
      userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36',
      userArn: null
    },
    path: '/prod/callJindo',
    protocol: 'HTTP/1.1',
    requestId: 'TDAOLjpLvHcEJsA=',
    requestTime: '18/Sep/2020:05:35:35 +0000',
    requestTimeEpoch: 1600407335479,
    resourceId: 'ANY /callJindo',
    resourcePath: '/callJindo',
    stage: 'prod'
  },
  pathParameters: null,
  stageVariables: null,
  body: '[{"jindoResult":1,"jindoTimeCalled":1600407334170}]',
  isBase64Encoded: false
}

Based on the error message it looks like there's no http object in requestContext , that's why it's failing to access the method property on it.

Taking a quick look at Set up Lambda proxy integrations in API Gateway we can notice the requestContext object is defined as:

    "requestContext": {
      "accountId": "12345678912",
      "resourceId": "roq9wj",
      "stage": "testStage",
      "requestId": "deef4878-7910-11e6-8f14-25afc3e9ae33",
      "identity": {
        "cognitoIdentityPoolId": null,
        "accountId": null,
        "cognitoIdentityId": null,
        "caller": null,
        "apiKey": null,
        "sourceIp": "192.168.196.186",
        "cognitoAuthenticationType": null,
        "cognitoAuthenticationProvider": null,
        "userArn": null,
        "userAgent": "PostmanRuntime/2.4.5",
        "user": null
      },
      "resourcePath": "/{proxy+}",
      "httpMethod": "POST",
      "apiId": "gy415nuibc"
    },

So in theory you just need to change your condition to:

    if (event.requestContext.httpMethod === "POST") {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM