0

Does anyone know, what can be the source of this error. I have no bools in register or check, but it is haunting me. Maybe anyone can share the tests chec50 does. This is ridiculous. There is no normal way to trace the error back. And yes, I cannot catch this error while manual testing my app.

Here is the code I use for register and check

@app.route("/register", methods=["GET", "POST"])
def register():
    if request.method == "POST":
        if not request.form.get('username'):
            return apology("must provide username", 400)
        if not request.form.get('password'):
            return apology("must provide password", 400)
        if not request.form.get('confirmation', 400):
            return apology("must provide password confirmation", 400)
        if request.form.get('password') != request.form.get('confirmation'):
            return apology('password and confirmation are not equal', 400)
        print('EXECUTING INSERT')
        result = db.execute(
            '''
                INSERT INTO
                users
                (username, hash)
                VALUES (:username, :hash)
            ''',
            username=request.form.get('username'),
            hash=generate_password_hash(request.form.get('password')))
        print(result)
        if not result:
            return apology("username already exists", 400)

        session["user_id"] = result

        return redirect("/")
    else:
        return render_template('register.html')

Here is the code I use for check


@app.route("/check", methods=["GET"])
def check():
    user_name = request.args.get('username', '')
    if len(user_name) == 0:
        return jsonify(False)
    matching_users = db.execute("SELECT * FROM users WHERE username = :username", username=user_name)
    if len(matching_users) == 0:
        return jsonify(True)
    else:
        return jsonify(False)

Here is my register html

{% extends "layout.html" %}

{% block title %}
    Log In
{% endblock %}

{% block main %}
    <form action="/register" method="post" id="registerForm">
        <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="Password (again)" type="password">
        </div>
        <button class="btn btn-primary" type="submit">Register In</button>
    </form>
    <script>
        document.addEventListener('submit', function(event) {
            event.preventDefault();
            var form = document.querySelector('form');
            var username = document.querySelector('[name="username"]');
            $.get('/check', {
                username: username.value
            }, function(res) {
                if (!username.value && !res) {
                    alert('Invalid username')
                } else if (res === false) {
                    alert('This username already exists');
                } else {
                    form.submit();
                }

            });
        })
    </script>
{% endblock %}

<code>enter image description here</code>

  • Do you get the same message from the "registering user succeeds" test? Suggests the problem is in index, because register calls index. – DinoCoderSaurus Dec 15 '19 at 16:48
  • @DinoCoderSaurus Yeah, I do, do you want to say, that index.html has an issue? – George.S Dec 15 '19 at 16:50
  • That's a python error; I mean the index function in application.py. – DinoCoderSaurus Dec 15 '19 at 19:34
  • @DinoCoderSaurus Thank you, your clue led me to finding that cs50.SQL client is not working really well when I was defining my queries with ''' SQL ''' (triple quotes string notation) – George.S Dec 16 '19 at 19:37

You must log in to answer this question.

Browse other questions tagged .