Home > OS >  I am getting a attribute error when I use find
I am getting a attribute error when I use find

Time:06-29

I'm making a terminal app that will login/signup using the firebase database and hashlib. I am trying to find the user and the hashed password into 2 variables but it keeps spitting out an error. I’ve installed all the packages but it is still giving an error.

Here is my code:

    import firebase_admin, hashlib
from firebase_admin import firestore
from firebase_admin import credentials

cred = credentials.Certificate("serviceAccountKey.json")
firebase_admin.initialize_app(cred)
db = firestore.client()
ask = input("Do you want to login or signup?\n")
if ask.lower() == "signup" or ask.lower() == "Signup":
    username = input("Please enter a username to signup.\n")
    password = input("Please enter a password to signup.\n")
    hashedpass = hashlib.md5(password.encode("utf-8")).hexdigest()
    print("Here is your password hash "   hashedpass  
          "Don't lose it or you won't be able to login.")
    data = {'user': username, 'password': hashedpass}
    db.collection("Data").add(data)
elif ask.lower() == "login" or ask.lower() == "Login":
    loginuser = input("What is your username?\n")
    loginusercheck = db.collection("Data").where("user", "==", loginuser).get()
    searchuser = loginusercheck.find(loginuser)
    if searchuser.lower() == 0:
      loginpass = input("What is your password hash?\n")
      loginpasscheck = db.collection("Data").where("password", "==", loginpass)
      searchpass = loginpasscheck.find(loginpass)
    if searchpass.lower() == 0:
      print("You've logged into your account.")
    elif searchpass.lower() == -1:
       exit(400)
    elif searchuser.lower() == -1:
      exit(400)

The error is:

Traceback (most recent call last):
File “main.py”, line 20, in <module>
 searchuser = loginusercheck.find(loginuser)
)
AttributeError: ‘list’ object has no attribute to ‘find’

CodePudding user response:

please follow the procedure i have solved the issue. you need not use find function here. you can try this procedure i hope it will work fine. thanks

import firebase_admin, hashlib
from firebase_admin import firestore
from firebase_admin import credentials

cred = credentials.Certificate("serviceAccountKey.json")
firebase_admin.initialize_app(cred)
db = firestore.client()
ask = input("Do you want to login or signup?\n")
if ask.lower() == "signup" or ask.lower() == "Signup":
    username = input("Please enter a username to signup.\n")
    password = input("Please enter a password to signup.\n")
    hashedpass = hashlib.md5(password.encode("utf-8")).hexdigest()
    print("Here is your password hash "   hashedpass  
          "Don't lose it or you won't be able to login.")
    data = {'user': username, 'password': hashedpass}
    db.collection("Data").add(data)
elif ask.lower() == "login" or ask.lower() == "Login":
    loginuser = input("What is your username?\n")
    loginusercheck = db.collection("Data").where("user", "==", loginuser).get()
    # searchuser = loginusercheck.find(loginuser)
    if loginuser in loginusercheck:
      loginpass = str(input("What is your password hash?\n"))
      loginpasscheck = db.collection("Data").where("password", "==", loginpass)
    #   searchpass = loginpasscheck.find(loginpass)
    if loginpass.lower() == 0:
      print("You've logged into your account.")
    elif loginpass.lower() == -1:
       exit(400)
    elif loginpass.lower() == -1:
      exit(400)

CodePudding user response:

I'm assuming that you are coming with a background in JavaScript since it seemed you try to use the Array.find() method. Python doesn't have this kind of syntax. Instead you could use @SaroarZahanSojib's answer to check whether or not the loginuser is in the loginusercheck list.

  • Related