🔗NameError: name 'MessageGroups' is not defined
In this troubleshooting session, I was met with a series of errors.
Challenge
- On the
Messages
page, the list of message groups was not showing. This was caused by a combination of multiple error codes here and there. This post lists the problems and solutions in the chronological sequence.
1. Import error
[2023-05-03 09:00:12,427] ERROR in app: Exception on /api/message_groups [GET]
Traceback (most recent call last):
...
File "/backend-flask/app.py", line 207, in data_message_groups
model = MessageGroups.run(cognito_user_id=cognito_user_id)
NameError: name 'MessageGroups' is not defined
192.168.160.10 - - [03/May/2023 09:00:12] "GET /api/message_groups HTTP/1.1" 500 -
Reason: the module (import statement) was missing in the file
app.py
for some reason.✅ Solution: import
message_groups.py
toapp.py
.from services.message_groups import *
2. Wrong parameter in the class MessagesGroups
[2023-05-03 09:08:27,784] ERROR in app: Exception on /api/message_groups [GET]
Traceback (most recent call last):
...
File "/backend-flask/app.py", line 208, in data_message_groups
model = MessageGroups.run(cognito_user_id=cognito_user_id)
TypeError: MessageGroups.run() got an unexpected keyword argument 'cognito_user_id'
192.168.160.10 - - [03/May/2023 09:08:27] "GET /api/message_groups HTTP/1.1" 500 -
Reason: when we update function's parameters, we often forget to update the function calls in other files.
✅ Updated the variable from
user_handle
tocognito_user_id
.# from class MessageGroups: def run(cognito_user_id): ... # to def run(cognito_user_id): ...
3. Bad naming of variables
[2023-05-03 09:14:57,032] ERROR in app: Exception on /api/message_groups [GET]
Traceback (most recent call last):
...
File "/backend-flask/app.py", line 208, in data_message_groups
model = MessageGroups.run(cognito_user_id=cognito_user_id)
File "/backend-flask/services/message_groups.py", line 29, in run
ddb = ddb.client()
UnboundLocalError: local variable 'ddb' referenced before assignment
Reason:
ddb
is already the name of a python file and the class it contains. Silly enough, I named my ddb client variable asddb
which should be initialised by the class of the same nameddb
(a single variable name had been used to refer to two different entities).✅ Update the variables with different names. In below code, both the ddb client instance and the ddb class are called equally
ddb.
from lib.ddb import ddb <----- class MessageGroups: def run(cognito_user_id): ... ddb = ddb.client() <---------- the problem data = ddb.list_message_groups(ddb, current_user_uuid)
✅ Change like this:
dynamodb \= ddb.client()
data = ddb.list_message_groups(dynamodb, current_user_uuid)
4. Incorrectly referencing undefined variable
[2023-05-03 09:38:02,243] ERROR in app: Exception on /api/message_groups [GET]
Traceback (most recent call last):
...
File "/backend-flask/app.py", line 208, in data_message_groups
model = MessageGroups.run(cognito_user_id=cognito_user_id)
File "/backend-flask/services/message_groups.py", line 33, in run
model['data'] = results
NameError: name 'results' is not defined
Source code
class MessageGroups:
def run(cognito_user_id):
printh("MessageGroups.run ...")
model = {
'errors': None,
'data': None <---- this becomes the chat room list on frontend.
}
...
data = ddb.list_message_groups( ... )
model['data'] = results <----- 'results' was never defined.
return model
Reason: accidentally, the word
results
was assigned as the value ofmodel['data'
] butresults
was never defined anywhere in the code.✅ Simply replace it with the real variable with actual values
data
.
Resolved - End result
- The message groups correctly displaying.