Week 5 Conversations with DynamoDB - TypeError: 'NoneType' object is not subscriptable
Table of contents
No headings in the article.
The problem I had with this unsubscriptable Nonetype
error was that the data was getting lost somewhere. So no matter how many times I click on the individual conversation groups, the data is not coming back so I couldn't see the conversation on the right column.
Every time I click on the conversation group (either Andrew Brown
or Worf
), it runs the following code in the backend:
# backend-flask/app.py
@app.route("/api/messages/<string:message_group_uuid>", methods=['GET'])
def data_messages(message_group_uuid):
access_token = jwttv.extract_access_token(request.headers)
try:
claims = jwttv.verify(access_token)
cognito_user_id = claims['sub']
model = Messages.run(
cognito_user_id = cognito_user_id,
message_group_uuid = message_group_uuid
)
The model
is created by the function Message.run
, the only function of the class Messages
in messages.py
. As it turns out, model
was missing in the return
statement.
# backend-flask/services/messages.py
class Messages:
def run(message_group_uuid, cognito_user_id):
printh("Messages.run() ...")
model = {
'errors': None,
'data': None
}
sql = db.template('users', 'uuid_from_cognito_user_id')
current_user_uuid = db.query_value(sql, {
'cognito_user_id': cognito_user_id
})
ddb_client = ddb.client()
data = ddb.list_messages(ddb_client, message_group_uuid)
model['data'] = data
return # <-------- return should return model
Modifying the return
line to return model
neatly solved the problem.
Simple enough? Yes, you just have to know where to look ๐.
ย