TypeError:在将JSON分配给python变量的GAE示例上,“NoneType”对象不可订阅

pxiryf3j  于 2023-01-01  发布在  Python
关注(0)|答案(2)|浏览(206)

当我将JSON赋值给一个python变量(如sheet_id = data["sheetID"])时,我收到了一个错误TypeError: 'NoneType' object is not subscriptable。它只发生在我的Google App Engine示例上。当我在本地运行Flask应用并使用Postman向应用发送POST请求时,我没有收到这个错误。以下是代码片段

@app.route('/main',methods=["POST"])
def main():
    data = request.json
    print(data)
    scope = [
    'https://www.googleapis.com/auth/spreadsheets',
    'https://www.googleapis.com/auth/gmail.send',
    'https://www.googleapis.com/auth/gmail.settings.basic',
    'https://www.googleapis.com/auth/gmail.settings.sharing'
    ]

    GCP_PROJECT = os.environ['GCP_PROJECT']
    GAE_SID_VERSION = os.environ['GAE_SID_VERSION']

    credentials1 = access_secret_version(GCP_PROJECT, <SECRET NAME>, GAE_SID_VERSION)
    credentials1 = json.loads(credentials1)
    credentials = service_account.Credentials.from_service_account_info(credentials1)
    scoped_credentials = credentials.with_scopes(scope)
    gc = gspread.authorize(scoped_credentials)

    #for sheet with categories and score
    sheet_id = data["sheetID"] #this gives me the error

    workbook = gc.open_by_key(sheet_id)
    ...

    ...
    return "OK"

奇怪的是,整个程序运行完成后,我收到了response = OK,我能够将来自GoogleSheets中的触发脚本的json数据发送到我的GAE示例,然后GAE示例将数据加载到Gcloud存储桶中。我从this link中了解到datasheet_id可以是None,但在这种情况下,我的整个程序如何运行到完成,甚至只是打开工作簿?这是Google Apps脚本

function runDockerImage(sheetID , mainWorksheet , emailTo , emailList , deliverToCloud ,filterOn , labelDisplay , sandbox , deliverToSlack) {
  // The code below logs the HTML code of the Google home page.
  var dataJSON = {
    'sheetID' :sheetID,
    'mainWorksheet' : mainWorksheet,
    'emailTo' : emailTo,
    'emailList' : emailList,
    'deliverToCloud' : deliverToCloud,
    'filterOn' : filterOn,
    'labelDisplay': labelDisplay,
    'sandbox' : sandbox,
    'deliverToSlack' : deliverToSlack 
    
  };
  
  payload = JSON.stringify(dataJSON)
  
  var headers = {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    'Authorization': 'Basic _authcode_'
  };
  
  var options = {
     'method': 'post',
     'contentType': 'application/json',
     'headers': headers,
     'payload': payload
  };
  
  
  var response = UrlFetchApp.fetch('https://<project-id>.uc.r.appspot.com/main', options);
  Logger.log(response);
 
}

以下是来自App Engine错误报告日志的跟踪:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/app/app.py", line 118, in main
    sheet_id = data["sheetID"]
TypeError: 'NoneType' object is not subscriptable

最后,我想问一下,这个程序怎么可能完全带着这个错误运行呢?我想如果我不修复这个错误,以后可能会遇到一些严重的问题。
错误报告日志

的屏幕截图

pgky5nke

pgky5nke1#

该错误意味着dataNone

$ python3
Python 3.8.3 (v3.8.3:6f8c8320e9, May 13 2020, 16:29:34) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> d=None
>>> d['3']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not subscriptable

奇怪的是,整个程序运行完成,我收到的响应= OK。
您是否确定没有错误处理程序或返回OK的东西,即使请求处理程序抛出了异常?
还有,你为什么要这么做?

dataJSON = request.json
json_string = json.dumps(dataJSON)
data = json.loads(json_string)

request.json将json请求有效负载作为python dict提供给您,但是您随后将其转换为json字符串,然后再次将其解析为python dict。

1hdlvixo

1hdlvixo2#

我知道这是一段时间为这篇文章,我有类似的问题,这个问题的原因是缩放(想想多个用户访问您的应用程序)这可能会导致意外的行为,全局/局部变量可能包含错误的信息,甚至可能是无。所以尝试托管您的应用程序首先只有1个示例,看看这个问题是否解决。如果是,那么原因是由于可扩展性。
顺便说一句。这个问题的解决方案,使您的应用程序无状态。所以不要长时间使用任何变量,将您的数据保存到另一个地方(例如,内存或任何其他数据库解决方案。

相关问题