I have some user response data that is posted from the front end to my /post end route and then one piece of that data (user id) is posted to another flask route /stats. That is working properly, when I print to the console I am getting the correct user_id I want to get. The problem is that in this /stats end route I am performing a query of the database to gather some important stats and then I am grabbing those on the front end through a get request. The problem is that when I perform the get request, the state of my request_data is lost and throws the error:
line 67, in stats
user = request_data['user']
TypeError: 'NoneType' object is not subscriptable
the app works perfectly until I perform the get request. here is my code for the flask route /stats:
@app.route('/stats', methods=['GET', 'POST'])
def stats():
request_data = request.get_json()
user = request_data['user']
def get_total_percent_correct(user_id):
correct = d.db_query('SELECT COUNT(*) FROM cards.responses WHERE guess = answer AND user_id = %s' % user_id)
total = d.db_query('SELECT COUNT(*) FROM cards.responses WHERE user_id = %s' % user_id)
return float(correct[0][0])/float(total[0][0])
response_body = {
"stat": get_total_percent_correct(user_id=0),
"user" : user
}
print(get_total_percent_correct(user_id=0))
print(request_data['user'])
return response_body
I would just make 2 if statements for get and post but the problem is I need the user object for my query data. right now the user_id is hardcoded in as 0 but I want to replace that with user object. I appreciate any help. thanks!