404错误在尝试更新Firebase Cloud Firestore数据库时发生。

huangapple go评论103阅读模式
英文:

404 Error when attempting to update Firebase Cloud Firestore database

问题

以下是您要翻译的内容:

I have a Firestore database like this:(https://i.stack.imgur.com/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.

  1. import firebase_admin
  2. from firebase_admin import credentials
  3. from firebase_admin import db
  4. # Initialize Firebase admin
  5. cred = credentials.Certificate('taiwaneew-firebase-adminsdk-odl9d-222bd18a4e.json')
  6. firebase_admin.initialize_app(cred, {
  7. 'databaseURL': 'https://taiwaneew.firebaseio.com/'
  8. })
  9. # Define a function to send data to the Firebase database
  10. def send_data(param1, param2):
  11. ref = db.reference(path='/TaiwanEEW/Event/1')
  12. ref.update({
  13. 'intensity': param1,
  14. 'seconds': param2
  15. })
  16. # Invoke our function to send data to Firebase
  17. send_data("test", 123)

The code, however, causes the following error:

  1. File "/Users/joelin/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/firebase_admin/db.py", line 929, in request
  2. return super(_Client, self).request(method, url, **kwargs)
  3. File "/Users/joelin/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/firebase_admin/_http_client.py", line 119, in request
  4. resp.raise_for_status()
  5. File "/Users/joelin/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/requests/models.py", line 1021, in raise_for_status
  6. raise HTTPError(http_error_msg, response=self)
  7. requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://taiwaneew.firebaseio.com/TaiwanEEW/Event/1.json?print=silent
  8. During handling of the above exception, another exception occurred:
  9. Traceback (most recent call last):
  10. File "/Users/joelin/PycharmProjects/pythonProject/eewPush.py", line 20, in <module>
  11. send_data("777", 778)
  12. File "/Users/joelin/PycharmProjects/pythonProject/eewPush.py", line 14, in send_data
  13. ref.update({
  14. File "/Users/joelin/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/firebase_admin/db.py", line 341, in update
  15. self._client.request('patch', self._add_suffix(), json=value, params='print=silent')
  16. File "/Users/joelin/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/firebase_admin/db.py", line 931, in request
  17. raise _Client.handle_rtdb_error(error)
  18. 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 have a Firestore database like this:(https://i.stack.imgur.com/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.

  1. import firebase_admin
  2. from firebase_admin import credentials
  3. from firebase_admin import db
  4. # Initialize Firebase admin
  5. cred = credentials.Certificate(&#39;taiwaneew-firebase-adminsdk-odl9d-222bd18a4e.json&#39;)
  6. firebase_admin.initialize_app(cred, {
  7. &#39;databaseURL&#39;: &#39;https://taiwaneew.firebaseio.com/&#39;
  8. })
  9. # Define a function to send data to the Firebase database
  10. def send_data(param1, param2):
  11. ref = db.reference(path=&#39;/TaiwanEEW/Event/1&#39;)
  12. ref.update({
  13. &#39;intensity&#39;: param1,
  14. &#39;seconds&#39;: param2
  15. })
  16. # Invoke our function to send data to Firebase
  17. send_data(&quot;test&quot;, 123)

The code, however, causes the following error:

  1. File &quot;/Users/joelin/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/firebase_admin/db.py&quot;, line 929, in request
  2. return super(_Client, self).request(method, url, **kwargs)
  3. File &quot;/Users/joelin/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/firebase_admin/_http_client.py&quot;, line 119, in request
  4. resp.raise_for_status()
  5. File &quot;/Users/joelin/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/requests/models.py&quot;, line 1021, in raise_for_status
  6. raise HTTPError(http_error_msg, response=self)
  7. requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://taiwaneew.firebaseio.com/TaiwanEEW/Event/1.json?print=silent
  8. During handling of the above exception, another exception occurred:
  9. Traceback (most recent call last):
  10. File &quot;/Users/joelin/PycharmProjects/pythonProject/eewPush.py&quot;, line 20, in &lt;module&gt;
  11. send_data(&quot;777&quot;, 778)
  12. File &quot;/Users/joelin/PycharmProjects/pythonProject/eewPush.py&quot;, line 14, in send_data
  13. ref.update({
  14. File &quot;/Users/joelin/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/firebase_admin/db.py&quot;, line 341, in update
  15. self._client.request(&#39;patch&#39;, self._add_suffix(), json=value, params=&#39;print=silent&#39;)
  16. File &quot;/Users/joelin/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/firebase_admin/db.py&quot;, line 931, in request
  17. raise _Client.handle_rtdb_error(error)
  18. 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.

答案1

得分: 0

I could not find the error here, so I come with a workaround.

你在这里没有找到错误,所以我提出一个解决方法。

You can use firebase_admin.firestore.

你可以使用 firebase_admin.firestore

Then, your db object will be instantiated by db = firestore.client(), and have access to all collections and documents (see doc).

然后,你的 db 对象将由 db = firestore.client() 实例化,并可以访问所有的集合和文档(参见文档)。

Complete solution:

完整解决方案:

  1. import firebase_admin
  2. from firebase_admin import credentials, firestore
  3. cred = credentials.Certificate('taiwaneew-firebase-adminsdk-odl9d-222bd18a4e.json')
  4. # no need for a URL here if your credentials already contain the project ID.
  5. firebase_admin.initialize_app(cred)
  6. db = firestore.client()
  7. # Define a function to send data to the Firebase database
  8. def send_data(param1, param2):
  9. doc = db.document('Event/1') # or doc = db.collection('Event').document('1')
  10. doc.update({
  11. 'intensity': param1,
  12. 'seconds': param2
  13. })
  14. # Invoke our function to send data to Firebase
  15. send_data("test", 123)
  1. <details>
  2. <summary>英文:</summary>
  3. I coud not find the error here, so I come with a workaround.
  4. You can use `firebase_admin.firestore`.
  5. Then, your `db` object will be instanciated by `db = firestore.client()`, and have access to all collections and document (see [doc][1]).
  6. ----------
  7. Complete solution:
  8. ```Python
  9. import firebase_admin
  10. from firebase_admin import credentials, firestore
  11. cred = credentials.Certificate(&#39;taiwaneew-firebase-adminsdk-odl9d-222bd18a4e.json&#39;)
  12. # no need for an url here if your credentials already contain the project id.
  13. firebase_admin.initialize_app(cred)
  14. db = firestore.client()
  15. # Define a function to send data to the Firebase database
  16. def send_data(param1, param2):
  17. doc = db.document(&#39;Event/1&#39;) # or doc = db.collection(&#39;Event&#39;).document(&#39;1&#39;)
  18. doc.update({
  19. &#39;intensity&#39;: param1,
  20. &#39;seconds&#39;: param2
  21. })
  22. # Invoke our function to send data to Firebase
  23. send_data(&quot;test&quot;, 123)

huangapple
  • 本文由 发表于 2023年2月9日 00:36:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75388908.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定