0

I have been struggling with the check function in PSET8 for a few weeks now, and have run into various problems as I tried to work through my issues. Currently, it seems by check .get function does not run.

Here is my JS code:

<script>
alert('Script Running');
var username=document.getElementById('username').value
document.getElementById("myForm").addEventListener("submit", function(event){
    event.preventDefault()
    alert('working');

    $.get("/check?username=" + encodeURIComponent(username), function(data) {
                if(data == false) {
                    alert("Username has been taken.");
                }
                else {
                    alert("Okay.");
                    ('myForm').submit();
                }
    });
});
</script>

As you can see, I have included an alert to notify me that the script is running; 'Script Running'. This works, but the $.get function never runs, and I do not receive any 'working' alert.

Here is my Python code for this function:

@app.route("/check", methods=["GET"])
def check():
    """Return false if username is not available, else true, in JSON format"""
    rows = db.execute("SELECT * FROM users WHERE username = :username", username = request.args.get("username"))
    print(rows)
    print('Running')
    if rows:
        return jsonify(False)
    else:
        return jsonify(True) 

I'm just about at wits end trying to get this working, so any help is appreciated. Below is the error I receive whenever registering a username that has already been used.

Note the RuntimeError: UNIQUE constraint failed: users.username

DEBUG:cs50:INSERT INTO users(username,hash)VALUES('cs50',     'pbkdf2:sha256:150000$sMzRwHl8$62ce3c92e6ca26a869218c0eae18ca6dce84323738ca04930d472778416dbf8d')
    ERROR:application:Exception on /register [POST]
    Traceback (most recent call last):
      File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
        response = self.full_dispatch_request()
      File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
        rv = self.handle_user_exception(e)
      File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
        reraise(exc_type, exc_value, tb)
      File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
        raise value
      File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
        rv = self.dispatch_request()
      File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
        return self.view_functions[rule.endpoint](**req.view_args)
      File "/home/ubuntu/environment/pset8/finance/application.py", line 212, in register
        username=request.form.get("username"),hash=hashpass
      File "/usr/local/lib/python3.7/site-packages/cs50/sql.py", line 21, in decorator
        return f(*args, **kwargs)
      File "/usr/local/lib/python3.7/site-packages/cs50/sql.py", line 361, in execute
        raise e
    RuntimeError: UNIQUE constraint failed: users.username
    INFO:werkzeug:192.168.113.226 - - [20/Jan/2020 10:52:29] "POST /register HTTP/1.0" 500 -
    INFO:werkzeug:192.168.113.226 - - [20/Jan/2020 10:52:29] "GET /static/styles.css HTTP/1.0" 200 -
    INFO:werkzeug:192.168.113.226 - - [20/Jan/2020 10:52:30] "GET /static/favicon.ico HTTP/1.0" 200 -

It appears the ID 'username' is unrecognized by my JS script, however, as you can see below I do have 'username' in my HTML.

<form action="/register" method="post" id="myForm">
    <div class="form-group">
        <input autocomplete="off" autofocus class="form-control" name="username" placeholder="Username" type="text">
    </div>
    <div class="form-group">
        <input class="form-control" name="password" placeholder="Password" type="password">
    </div>
    <div class="form-group">
        <input class="form-control" name="confirmation" placeholder="Confirm Password" type="password">
    </div>
    <button class="btn btn-primary" type="submit">Register</button>
</form>
9
  • Have you checked the browser console for errors? – DinoCoderSaurus Jan 20 '20 at 12:40
  • Good point: It looks like I'm receiving an error on the line: var username=document.getElementById('username').value. Error reads: Uncaught TypeError: Cannot read property 'value' of null at HTMLFormElement.<anonymous> (register:69) Interesting though, as my html does have an elementID named 'username'. I have added my HTML to the post. – KullideDev Jan 21 '20 at 13:56
  • Yeah but, getElementById doesn't use the "name" property. Possible solution in the MDN doc linked here. – DinoCoderSaurus Jan 21 '20 at 14:16
  • Sorry @DinoCoderSaurus , I think you may have forgotten the link! – KullideDev Jan 23 '20 at 10:32
  • Clicking on the link (getElementById) works for me! in any case developer.mozilla.org/en-US/docs/Web/API/Document/… is the URL. – DinoCoderSaurus Jan 23 '20 at 12:14

You must log in to answer this question.

Browse other questions tagged .