thisbejim / Pyrebase

A simple python wrapper for the Firebase API.
2.07k stars 529 forks source link

AttributeError when referencing database #258

Closed DannyDannyDanny closed 3 years ago

DannyDannyDanny commented 6 years ago

Prerequisites

My Code

I'm just trying to get firebase <-> python to talk. I run the code using Python3 firebaseconn.py

import pyrebase

# I've temporarily deleted the credentials
config = dict()
email = "eml@dmn.com"
password = "password"

firebase = pyrebase.initialize_app(config)
auth = firebase.auth()
user = auth.sign_in_with_email_and_password(email, password)

# Next line is where shit hits the fan and also line 21 in my real script
db = firebase.database()

# I dont know if this will run
db.child("users").child("Morty")
print("nice")

The error I receive

Traceback (most recent call last):
  File "python/firebase/firebasecon.py", line 21, in <module>
    db = firebase.database()
  File "/usr/local/lib/python3.6/site-packages/pyrebase/pyrebase.py", line 66, in database
    return Database(self.credentials, self.api_key, self.database_url, self.requests)
  File "/usr/local/lib/python3.6/site-packages/pyrebase/pyrebase.py", line 171, in __init__
    if not database_url.endswith('/'):
AttributeError: 'tuple' object has no attribute 'endswith'

Guidance is appreciated 😽

Thank you for helping make Pyrebase better!

zachyam commented 5 years ago

@DannyDannyDanny Were you able to figure out the issue? I'm running into a similar issue now

DannyDannyDanny commented 5 years ago

@zachyam Nope. I have a feeling this repo is no longer maintained. This did reignite my interest in connecting python to firebase again and I'm watching google's firecast tutorial for python.

zachyam commented 5 years ago

@DannyDannyDanny

Not sure if this is useful, but the way I got the error was trying to initialize my own Firebase class, like so:

import firebaseconfig
class Firebase():
    def __init__(self):
        self.firebase_config = {
            'apiKey': firebaseconfig.DATABASE_CONFIG['apiKey'],
            'authDomain': firebaseconfig.DATABASE_CONFIG['authDomain'],
            'databaseURL': firebaseconfig.DATABASE_CONFIG['databaseURL'],
            'projectId': firebaseconfig.DATABASE_CONFIG['projectId'],
            'storageBucket': firebaseconfig.DATABASE_CONFIG['storageBucket'],
            'messagingSenderId': firebaseconfig.DATABASE_CONFIG['messagingSenderId'],
        }
        self.firebase_app = pyrebase.initialize_app(self.firebase_config)
        self.firebase_db = self.firebase_app.database()

        def delete_user_db(self, chat_id, member):
          try:
              self.firebase_db.child(str(chat_id)+"/"+"members"+"/"+str(member.id)).remove()
          except Exception as error:
              print('Could not delete user from db', error)
          return 'OK'

I took out the self.firebase_db = self.firebase_app.database() from the __init__ and instead put it inside the delete_user_db function, like so:

def delete_user_db(self, chat_id, member):
  firebase_db = self.firebase_app.database()
  try:
    firebase_db.child(str(chat_id)+"/"+"members"+"/"+str(member.id)).remove()
  except Exception as error:
    print('Could not delete user from db', error)
  return 'OK'

It worked after that.

If what you tried was exactly what your comment said, then I'm not sure, as nothing seems wrong with it

DannyDannyDanny commented 5 years ago

@zachyam I managed to make it work with google's own python library. See Code.

DannyDannyDanny commented 3 years ago

Link dead. Code gone.

ghost commented 9 months ago

is there still any solution for this?