如何检查数据是否存在于 firebase 中。 (KivyMD Python)

How to check if data exist in firebase. (KivyMD Python)

我是 PythonKivyMD 的新手。也可以使用数据库。我想检查用户使用 KivyMD 应用程序提供的数据是否已经在 Firebase 实时数据库中。这些是 firebase 中的数据。

代码

def send_data(self, email):
    from firebase import firebase

    firebase = firebase.FirebaseApplication("https://infinity-mode-default-rtdb.firebaseio.com/", None)

    data = {
        'Email' : email
    }

    if email.split() == []:
        cancel_btn_checkpoint_dialogue = MDFlatButton(text='Retry', on_release=self.close_checkpoint_dialogue)
        self.checkpoint_dialog = MDDialog(title='Access Denied', text="Invalid Username"),
                                          buttons=[cancel_btn_checkpoint_dialogue])

        self.checkpoint_dialog.open()

    else:
        firebase.post('Users', data)

如果用户输入数据库中的现有值,则不应将该值保存在数据库中。还应显示一个对话框,表明该电子邮件已在使用中。如果用户提供的值不在数据库中,则应将其保存。请帮我做一下。

尝试使用 dataSnapshot.exist() 方法或 snapshot.hasChild 方法,它们适用于 python,我不能 100% 确定它们是否适用于 python 但它值得一去

我猜您使用的是 python-firebase,目前它的文档很少。所以我会根据包源尽量做到最好...

您必须使用 firebase.get 方法来检索包含为给定用户存储的当前数据的字典,然后检查该数据是否包含您要添加的信息(未经测试,因为 firebase 不甚至在我的机器上导入):

record = firebase.get('/Users', 'id')
if not record.get('Email'):
    firebase.post('/Users/'+'id', data)

你试过 firebase_admin 了吗?

我这样检查我的数据:

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore

cred = credentials.Certificate(
        "firestore-cred.json"
    )
firebase_admin.initialize_app(cred)
db = firestore.client()
data = {
    "Email" : email
}

query_email = db.collection(u'Users') \
                .where(u"Email",u"==",data["Email"]) \
                .get()
if query_email:  
    # exists
    ...
else:
    # does not exist
    ...

如果用户邮箱不存在,query_email 将为空列表。 不要忘记 query_email 不是 json 数据。您可以将其转换为 json 和 to_dict():

email_query_result_as_json = query_email[0].to_dict()