thisbejim / Pyrebase

A simple python wrapper for the Firebase API.
2.05k stars 526 forks source link

IndexError: list index out of range when Pyrebase does not return a response #317

Open ruffy2 opened 4 years ago

ruffy2 commented 4 years ago

I'm querying my db and not getting any response back. I am using the following query:

response_object = db.child(key).order_by_child(child).equal_to(id).get()

I want to check to see if I get a response. If I do, do something. If not, say I could not find a match:

if response_object.val() is not None:
                // do something
else:
                print('Could not find matching transaction')

I have used this pattern before successfully but this time I am getting an IndexError. Any idea what is wrong? Thanks for the help

File "/Users/rafilurie/Projects/cash/flaskvue/get_new_plaid_transactions.py", line 473, in remove_pending_transactions_from_db
    if matching_pending_transaction_response_object.val() is not None:
  File "/Users/rafilurie/Projects/cash/flaskvue/backend/venv/lib/python3.7/site-packages/pyrebase/pyrebase.py", line 537, in val
    if isinstance(self.pyres[0].key(), int):
IndexError: list index out of range
alpsilva commented 2 years ago

I have encountered the same issue. I noted that the pattern that worked for me, when querying an empty database, had the following term attached:

.limit_to_first(number)

Even though it didn't make much sense for me to limit the result of the query giving me errors, I put the term there with a high enough number, and it worked. Hope this answer can help someone dealing with the same problem in the future!

Complete code for reference:

db = pyrebase.initialize_app(pyrebase_config).database()

response = (
    db.child(key)
    .order_by_child("some_firebase_key")
    .equal_to(comparison_key)
    .limit_to_first(number)
    .get()
)