0

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!

  • What's the JSON supposed to be in the GET request? This route looks like a pure POST route. – ggorlen Aug 12 '22 at 21:30
  • The JSON is {"totalAccuracy":0.3076923076923077,"user":{"user":916}} – thealchemist7777 Aug 12 '22 at 21:32
  • OK, but how is that supposed to be added to a GET request in such a way that `.get_json()` works? It's sort of a rhetorical question--I'm suggesting that it won't work and that you might have a misunderstanding en route to achieving whatever you're trying to acheive. – ggorlen Aug 12 '22 at 21:42
  • I'm confused on what you're asking. my logic was I could post the user object from /post to /stats, which I have and that works. Now I want to get the totalAccuracy query result to the front end which works when I hard code in the user id but I want it to take the object "user" as its parameter instead. I'm confused because I posted the user object to the route in which I'd like to get it from, but the value is wiped whenever I perform the get request. – thealchemist7777 Aug 12 '22 at 21:51
  • If this is data you want to send from the backend to the frontend, it should probably be sent in the response to the POST request. GET request doesn't retrieve the value from a POST request, or something like that, if that's what you're thinking. Your POST request would say "give me whatever data for userId whatever" and the response would be "here's whatever data you requested for userId whatever". Or you can use a GET route to achieve the same thing, but the request would probably use route or query parameters, not JSON. – ggorlen Aug 12 '22 at 21:59
  • Ok, so how do i retrieve the value from the post request and get it to the front end without using a GET request? I understand what you're saying but am not sure how to go about it – thealchemist7777 Aug 12 '22 at 22:44
  • Ok, but that still gives the same error. for some reason it is still making a get request – thealchemist7777 Aug 12 '22 at 23:03
  • My mistake--that should be `fetch(url, {method: "post", headers: {'Accept': 'application/json', 'Content-Type': 'application/json'}, body: JSON.stringify(user))` or whatever your payload is. See [POST Request with Fetch API?](https://stackoverflow.com/questions/39565706/post-request-with-fetch-api) – ggorlen Aug 12 '22 at 23:04

0 Answers0