简体   繁体   中英

How can I solve : TypeError: 'NoneType' object is not iterable

I have a Python Flask code and here is this error for me: TypeError: 'NoneType' object is not iterable. Thus, in the app there are two types of sessions: pro and common, if the user's email is in the db pro list it will be redirected to a screen, otherwise it will be redirected to it.

    ###########################
    conn = mysql.connector.connect(host="localhost",
                              database="*********", user="root",
                              password="********")

    ku = conn.cursor()

    em = ku.execute("SELECT Email FROM pro WHERE email=%s",(emails,))
    emm = ku.fetchone()
    
    
    nms2 = [str(i) for i in emm]
    emai = str("".join(nms2))
    

    ###########################
    if resposta:
        session["loggedin"] = True
        session["email"] = resposta[1]
        if emails in emai:
            return redirect(url_for("home_pro"))
        
        return redirect(url_for("home"))
    else:
        msg="Email/Senha incorretos"

    ###########################

How can I resolve this error? In Python, the error is on the line: nms2 = [str(i) for i in emm]

Check if the emm is empty/None before running a for loop on it.

fetchone() method gives the row if required row exist in database. there is no row in database where your condition is matched. You can handle this error like this:

if emm is not None:
    nms2 = [str(i) for i in emm]
    emai = str("".join(nms2))
else:
    print("No row found with that email")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM