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

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

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.

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 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.

import firebase_admin
from firebase_admin import credentials
from firebase_admin import db

# Initialize Firebase admin
cred = credentials.Certificate(&#39;taiwaneew-firebase-adminsdk-odl9d-222bd18a4e.json&#39;)
firebase_admin.initialize_app(cred, {
    &#39;databaseURL&#39;: &#39;https://taiwaneew.firebaseio.com/&#39;
})

# Define a function to send data to the Firebase database
def send_data(param1, param2):
    ref = db.reference(path=&#39;/TaiwanEEW/Event/1&#39;)
    ref.update({
        &#39;intensity&#39;: param1,
        &#39;seconds&#39;: param2
    })

# Invoke our function to send data to Firebase
send_data(&quot;test&quot;, 123)

The code, however, causes the following error:

    File &quot;/Users/joelin/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/firebase_admin/db.py&quot;, line 929, in request
        return super(_Client, self).request(method, url, **kwargs)
    File &quot;/Users/joelin/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/firebase_admin/_http_client.py&quot;, line 119, in request
        resp.raise_for_status()
    File &quot;/Users/joelin/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/requests/models.py&quot;, 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 &quot;/Users/joelin/PycharmProjects/pythonProject/eewPush.py&quot;, line 20, in &lt;module&gt;
        send_data(&quot;777&quot;, 778)
    File &quot;/Users/joelin/PycharmProjects/pythonProject/eewPush.py&quot;, line 14, in send_data
        ref.update({
    File &quot;/Users/joelin/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/firebase_admin/db.py&quot;, line 341, in update
        self._client.request(&#39;patch&#39;, self._add_suffix(), json=value, params=&#39;print=silent&#39;)
    File &quot;/Users/joelin/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/firebase_admin/db.py&quot;, 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.

答案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:

完整解决方案:

import firebase_admin
from firebase_admin import credentials, firestore

cred = credentials.Certificate('taiwaneew-firebase-adminsdk-odl9d-222bd18a4e.json')
# no need for a 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)

<details>
<summary>英文:</summary>

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][1]).


----------


Complete solution:

```Python
import firebase_admin
from firebase_admin import credentials, firestore


cred = credentials.Certificate(&#39;taiwaneew-firebase-adminsdk-odl9d-222bd18a4e.json&#39;)
# 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(&#39;Event/1&#39;) # or doc = db.collection(&#39;Event&#39;).document(&#39;1&#39;)
    doc.update({
        &#39;intensity&#39;: param1,
        &#39;seconds&#39;: param2
    })

# Invoke our function to send data to Firebase
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:

确定