2016-09-27 77 views
-2

我有一个方法,我检查用户是否存在于数据库中,我想将该方法结果传递给另一个方法,但无法这样做,错误我看到的是“类型错误:‘布尔’对象不是可调用的”不能从一种方法返回布尔值

这里是我的代码:

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 

而这正是我试图调用aboves方法结果

@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) 

非常感谢即

+0

您在那个'else'分支中缺少一个'return',但那不会引起您当前拥有的错误。我期望'inDB'有一个'NameError'。你可以显示你的完整回溯 –

+1

你也应该在'inDB == userInDb(username,email,password)'行中使用单个'='而不是双倍 – valignatev

+0

@valentjedi是的,我注意到只有在复制代码后才发现错误。谢谢! – limeThyme

回答

0

你忘记返回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 

你正在测试inDB == userInDb(...)而不是分配inDB = userInDb(...)

你也可以做if inDB:代替if inDB is 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() 

    return bool(data) # just return bool of data 

将会更清洁

+1

他们还测试'inDB == userInDb(...)'而不是分配,并测试'如果inDB是True:'而不是'如果inDB:'(后者没有错,只是皱起眉头)。 – ShadowRanger

+0

谢谢,现在它工作。我调整了另一种方法返回布尔(数据) - 不知道这个技巧。非常感激。 – limeThyme

+0

@limeThyme不客气。如果此问题解决了您的问题,请将此帖标记为答案,以便用户看到此问题不再需要他们的帮助。 –