简体   繁体   English

错误] TypeError: 'NoneType' object 不可下标

[英]ERROR] TypeError: 'NoneType' object is not subscriptable

So I was trying to make a serverless API using aws gateway , lambda and dyamodb for a movie database.因此,我尝试使用aws gatewaylambdadyamodb为电影数据库制作无服务器 API 。 The post part of the api seems to work fine it's the get part that is not working. api 的 post 部分似乎工作正常,但 get 部分不起作用。 I get the followinng error on postman while testing:我在测试时在postman上收到以下错误:

got this error on the postman with error code 502postman上出现此错误,错误代码为502

{
    "message": "Internal server error"
}

My cloudwatch generated this error:我的 cloudwatch 产生了这个错误:

ERROR] TypeError: 'NoneType' object is not subscriptable
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 32, in lambda_handler
    response = getMovie(event['queryStringParameters']['movie_id'])

My code snippet:我的代码片段:

def lambda_handler(event,context):
   logger.info(event)
   httpMethod=event['httpMethod']
   path=event['path']
if httpMethod==getMethod and path== healthPath:
    response=buildResponse(200)
elif httpMethod==getMethod and path == moviePath:
    response=getMovie(event['queryStringParameters']['movie_id'])
elif httpMethod==postMethod and path==moviePath:
    response=saveMovie(json.loads(event['body']))
elif httpMethod==patchMethod and path==moviePath:
    requestBody=json.loads(event['body'])
    response=modifyMovie(requestBody['movie_id'],requestBody['updateKey'],requestBody['updateValue'])
elif httpMethod==deleteMethod and path==moviePath:
    requestBody=json.loads(event['body'])
    response=deleteMovie(requestBody['movie_id'])
else:
    
    response=buildResponse(404,"Error Generated")
return response

Logger info says this: logger.info(event) Does anyone has a clue how to navigate out of this?记录器信息是这样说的: logger.info(event)有人知道如何摆脱这个吗? Thank you!谢谢!

You should probably rewrite your code to use event.get('...') .您可能应该重写代码以使用event.get('...') This will probably not correct your problem but it will make it easier to figure out what is wrong.这可能不会纠正你的问题,但它会让你更容易找出问题所在。

def lambda_handler(event,context):
   logger.info(event)
   httpMethod = event.get('httpMethod')
   path = event.get('path')
   if httpMethod == getMethod and path == healthPath:
     response = buildResponse(200)
   elif httpMethod == getMethod and path == moviePath:
     params = event.get('queryStringParameters')
     if params is None:
         return buildResponse(400, "queryStringParameter is None!")
      
     movie_id = params.get('movie_id')
     if movie_id is None:
         return buildResponse(400, "movie_id is None!")


     ... 

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

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