NameError: name 'user_id' is not defined. For no apparent reason : <p><strong>Error description</strong></p>

<p>I would fire up the server program, and fire a request (GET) at the path <code>/test_connection</code> which is the path used by the client to test the server IP address btw. The server would then respond by printing out the traceback below in the console, the one line in particular that I can't get my head around is <code>NameError: name 'user_id' is not defined</code> as the variable is not even included in the response function for the path <code>/test_connection</code> (<code>connection_test()</code>). I know the error is on the server side as it will print out 'INTERNAL SERVER ERROR: 500' on the client code, so I didn't feel the necessity to include the client code.</p>

<p><strong>Additional info:</strong></p>

<ul>
<li>Python version: 3.7</li>
<li>OS: Windows 10</li>
<li>Server Library: Flask</li>
</ul>

<p><strong>Traceback from error:</strong></p>

<pre><code>  Traceback (most recent call last):
  File "C:\Users\sccre\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 2309, in __call__
  return self.wsgi_app(environ, start_response)
  File "C:\Users\sccre\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 2295, in wsgi_app
  response = self.handle_exception(e)
  File "C:\Users\sccre\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1741, in handle_exception
  reraise(exc_type, exc_value, tb)
  File "C:\Users\sccre\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\_compat.py", line 35, in reraise
  raise value
  File "C:\Users\sccre\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 2292, in wsgi_app
  response = self.full_dispatch_request()
  File "C:\Users\sccre\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
  rv = self.handle_user_exception(e)
  File "C:\Users\sccre\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
  reraise(exc_type, exc_value, tb)
  File "C:\Users\sccre\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\_compat.py", line 35, in reraise
  raise value
  File "C:\Users\sccre\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
  rv = self.dispatch_request()
  File "C:\Users\sccre\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1799, in dispatch_request
  return self.view_functions[rule.endpoint](**req.view_args)
  File "E:\USB Stick Files\Programming\Python\Chat Room Thingy\server.py", line 19, in connection_test
  message_dict[user_id]
  NameError: name 'user_id' is not defined
</code></pre>

<p><strong>Server code</strong></p>

<pre><code>from flask import Flask, request
from threading import Thread
import json

app = Flask(__name__)

message_dict = {}
ids = -1


@app.route('/test_connection')
def connection_test():
    global ids

    print('Connection tested from: {}'.format(request.remote_addr))

    ids += 1
    id_sent = str(ids)
    return '{}'.format(id_sent)

@app.route('/new_message', methods=['POST'])
def new_message():
    global message_dict

    message_text = request.form['message']
    user_id = request.form['id']

    try:
        message_dict[user_id]['message'] = message_text
    except:
        message_dict[user_id] = None
        message_dict[user_id]['message'] = message_text

    return '0'

@app.route('/chat')
def chat():
    return json.dumps(message_dict)

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')
</code></pre>

<p>Any help with this would be greatly appreciated.</p>

<p>Oscar.</p>
