limeThyme limeThyme - 2 months ago 11
Python Question

Can't return a boolean from one method to another

I have a method in which I check if a User exists in the database and I want to pass that methods result to another method, but am unable to do so, the error I am seeing is 'TypeError: 'bool' object is not callable'

Here is my code:

def userInDb(username, email, password):
cursor = mysql.get_db().cursor()
cursor.execute('SELECT userID from user WHERE userName = %s and email = %s and password = %s', [username, email, password])
data = cursor.fetchone()

if data is None:
return False
else:
True


And this is where I am trying to call the aboves method result

@app.route('/showSignIn', methods=["POST"])
def showSignInPost():
username = request.form['inputUserName'] # values received from the UI input by users
email = request.form['inputEmail']
password = request.form['inputPassword']

inDB == userInDb(username, email, password)

if inDB is True:
return render_template('signin.html', signInHeader = "User doesn't exist, please try again")
else:
return render_template('signin.html', signInHeader = "Welcome " + username )


Thanks in advance.

Answer

You forgot to return True

def userInDb(username, email, password):
    cursor = mysql.get_db().cursor()
    cursor.execute('SELECT userID from user WHERE userName = %s and email = %s and password = %s', [username, email, password])
    data = cursor.fetchone()

    if data is None:
        return False
    else:
        return True #you forgot return

And you are testing inDB == userInDb(...) instead of assigning inDB = userInDb(...)

Also you can just do if inDB: instead of if inDB is True:

And if you do this

def userInDb(username, email, password):
    cursor = mysql.get_db().cursor()
    cursor.execute('SELECT userID from user WHERE userName = %s and email = %s and password = %s', [username, email, password])
    data = cursor.fetchone()

    return bool(data) # just return bool of data

will be much cleaner