我有一个如下所示的Firestore数据库:(https://i.stack.imgur.com/QSZ8m.png)
我的代码打算用值“test”和123分别更新字段“intensity”和“seconds”(在文档“1”下,在集合“Event”下)。
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)
但是,该代码会导致以下错误:
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
我已经尝试找出错误的原因,但同样的错误仍然存在。我真的很想听听一些意见,如果你有任何经验,在这方面。谢谢你这么多!
我已经仔细检查了我的凭据json文件是正确的,与python文件在同一目录下,并且我的数据库读写权限设置为true。
我尝试了“/TaiwanEEW/Event/1”和“/taiwaneew/Event/1”作为引用路径,因为我不确定它应该是项目名称还是数据库名称。
1条答案
按热度按时间njthzxwz1#
我在这里找不到错误,所以我提供了一个变通方案。
您可以使用
firebase_admin.firestore
。然后,您的
db
对象将由db = firestore.client()
示例化,并且可以访问所有集合和文档(请参见doc)。完整解决方案: