【问题标题】:'NoneType' object has no attribute 'get': AttributeError“NoneType”对象没有属性“get”:AttributeError
【发布时间】:2020-06-23 13:02:07
【问题描述】:

我收到错误:“NoneType”对象没有属性“get”:AttributeError。请帮我弄清楚。

文件“/var/task/lambda_function.py”,第 15 行,在 lambda_handler 中 create_stripe_customer(cardData,phone,email)

import json
import boto3
import stripe
client = boto3.client('secretsmanager')
keys = json.loads(client.get_secret_value(
  SecretId = 'arn:aws:secretsmanager:ap-so',
)['SecretString'])
public_key = keys['stripe-public']
secret_key = keys['stripe-secret']
stripe.api_key = secret_key
def lambda_handler(event, context):
    cardData = event.get('cardData')
    phone = event.get('phone')
    email = event.get('email')
    create_stripe_customer(cardData,phone,email)
    return event

def create_stripe_customer(payment_info, phone, email):
    customer_id = stripe.Customer.create( phone = phone,email=email)['id']
    payment_method_id = create_payment_method(payment_info)
    stripe.PaymentMethod.attach(payment_method_id, customer = customer_id)
    return {"customer_id": customer_id,
            "plan": create_stripe_plan(customer_id)
    }

def create_payment_method(payment_info):
    return stripe.PaymentMethod.create(type = "card",
    card = {"number": payment_info.get('cardNumber'),
            "exp_month": payment_info.get('expirationMonth'),
            "exp_year": payment_info.get('expirationYear'),
            "cvc": payment_info.get('ccv'),
            }).get('id')      
        
def create_stripe_plan(customer_id):
    return stripe.Subscription.create(
        customer = customer_id,items = [{"plan": "plan_idxxxxxxx"}]).get("id")

【问题讨论】:

  • event的结构是什么?
  • 请显示您的呼叫地址lambda_handler
  • 我通过 aws api 调用它。
  • 您是否在 api 网关中使用代理集成?
  • 不,我没有使用代理集成。

标签: python amazon-web-services aws-lambda stripe-payments


【解决方案1】:

为什么它不起作用


event.get() 可能是为检索事件中的数据而进行的错误调用类型。因此,数据类型将被收集并保存为NoneType

引用here,你可以通过调用event['item']而不是event.get('item')来获取事件项。

这是一个例子:

def my_handler(event, context):
    message = 'Hello {} {}!'.format(event['first_name'], 
                                    event['last_name'])  
    return { 
        'message' : message
    }  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-10
    • 1970-01-01
    • 2022-07-21
    • 2020-01-25
    • 2019-08-14
    • 1970-01-01
    • 2019-01-01
    • 2021-12-26
    相关资源
    最近更新 更多