I have a Firestore database like this:(https://i.sstatic.net/QSZ8m.png)
My code intends to update the fields "intensity" and "seconds" (under the document "1", under collection "Event") with the value "test" and 123 respectively.
import firebase_admin
from firebase_admin import credentials
from firebase_admin import db
# Initialize Firebase admin
cred = credentials.Certificate('taiwaneew-firebase-adminsdk-odl9d-222bd18a4e.json')
firebase_admin.initialize_app(cred, {
'databaseURL': 'https://taiwaneew.firebaseio.com/'
})
# Define a function to send data to the Firebase database
def send_data(param1, param2):
ref = db.reference(path='/TaiwanEEW/Event/1')
ref.update({
'intensity': param1,
'seconds': param2
})
# Invoke our function to send data to Firebase
send_data("test", 123)
The code, however, causes the following error:
File "/Users/joelin/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/firebase_admin/db.py", line 929, in request
return super(_Client, self).request(method, url, **kwargs)
File "/Users/joelin/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/firebase_admin/_http_client.py", line 119, in request
resp.raise_for_status()
File "/Users/joelin/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/requests/models.py", line 1021, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://taiwaneew.firebaseio.com/TaiwanEEW/Event/1.json?print=silent
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/joelin/PycharmProjects/pythonProject/eewPush.py", line 20, in <module>
send_data("777", 778)
File "/Users/joelin/PycharmProjects/pythonProject/eewPush.py", line 14, in send_data
ref.update({
File "/Users/joelin/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/firebase_admin/db.py", line 341, in update
self._client.request('patch', self._add_suffix(), json=value, params='print=silent')
File "/Users/joelin/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/firebase_admin/db.py", line 931, in request
raise _Client.handle_rtdb_error(error)
firebase_admin.exceptions.NotFoundError: 404 Not Found
I have tried to identify the cause of error but the same error persists. I would really like to hear some opinions if you have any experiences on this. Thank you so much!
I have double checked that my credentials json file is correct, under the same directory as the python file, and my database premissions to write and read set to true.
I tried both '/TaiwanEEW/Event/1' and '/taiwaneew/Event/1' for the reference path because I am not sure if it should be the project name or the database name.
I coud not find the error here, so I come with a workaround.
You can use firebase_admin.firestore
.
Then, your db
object will be instanciated by db = firestore.client()
, and have access to all collections and document (see doc).
Complete solution:
import firebase_admin
from firebase_admin import credentials, firestore
cred = credentials.Certificate('taiwaneew-firebase-adminsdk-odl9d-222bd18a4e.json')
# no need for an url here if your credentials already contain the project id.
firebase_admin.initialize_app(cred)
db = firestore.client()
# Define a function to send data to the Firebase database
def send_data(param1, param2):
doc = db.document('Event/1') # or doc = db.collection('Event').document('1')
doc.update({
'intensity': param1,
'seconds': param2
})
# Invoke our function to send data to Firebase
send_data("test", 123)