简体   繁体   English

Lambda 给了我调用错误 - 无法读取未定义的属性“方法”

[英]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.我有我创建的这个 lambda 函数,无论出于何种原因它都不起作用。 I have multiple other queries I have created in lambda connecting and updating Dynamo db.我在 lambda 连接和更新 Dynamo 数据库中创建了多个其他查询。 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 STRINGIFY JSON 数据通过 AJAX 发布到 LAMBDA

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

Error that I am getting in lambda logs我在 lambda 日志中遇到的错误

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.根据错误消息, requestContext似乎没有http对象,这就是它无法访问其上的方法属性的原因。

Taking a quick look at Set up Lambda proxy integrations in API Gateway we can notice the requestContext object is defined as:快速查看在 API Gateway设置 Lambda 代理集成,我们可以注意到requestContext对象定义为:

    "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") {

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 请求调用给我 TypeError:无法读取未定义的属性“statusCode” - request call giving me TypeError: Cannot read property 'statusCode' of undefined 无法读取猫鼬方法中未定义错误的属性 - Cannot read property of undefined error in mongoose method 我正在尝试为我的 discord 机器人发出轮询命令,但它一直给我错误:“TypeError: Cannot read property 'push' of undefined” - I'm trying to make a poll command for my discord bot, but it keeps giving me the error: “TypeError: Cannot read property 'push' of undefined” 给出“无法读取未定义的属性'密码'”而不是处理错误 - Giving "Cannot read property 'password' of undefined" instead of handling the error 出现此错误的原因是什么:无法读取未定义的属性“发送” - What is the reason for giving this error: cannot read property 'send' of undefined 读取 json 文件时,我的节点 js 程序给了我一个“TypeError: Cannot read property 'then' of undefined” - My node js program is giving me a “TypeError: Cannot read property 'then' of undefined”, when reading a json file AWS Lambda:“无法读取未定义的属性‘0’” - AWS Lambda: "Cannot read property '0' of undefined" “TypeError:无法读取未定义的属性‘我’” - “TypeError: Cannot read property 'me' of undefined” 错误无法读取未定义的属性“ 0” - Error Cannot read property '0' of undefined 错误:无法读取未定义的属性 - error: Cannot read property of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM