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

Viewed 45

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]

2 Answers

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")
Related