请使用python监视您的防火墙数据库,以便在添加新元素时执行特定任务。喜欢经常检查数据库中是否有新的或传入的元素;检查是否向数据库中添加了一个新元素。
发布于 2022-10-09 15:10:33
您可以使用listen的firebase-admin方法来侦听对火基实时数据库的特定字段所做的更改。
以下是完整的示例:
# Importing required packages
import firebase_admin
from firebase_admin import credentials, db
# Initializing the database with URL
cred = credentials.Certificate('path/to/your/service.json')
firebase_admin.initialize_app(cred,{
'databaseURL': 'https://YOUR-DATABASE-URL-NAME-HERE.firebaseio.com/'
})
def listener(event):
print(event.event_type) # Indicates the type of the event done
print(event.path) # References the path of the event
print(event.data) # Gives the UPDATED DATA, None if deleted
# Now, executing specific functions based on the new data(UPDATED DATA)
if str(event.data) == 'hello':
# /// Execute your code here ///
print('Data updated as ', str(event.data))
elif str(event.data) == None:
# /// Execute your code here ///
print('Your data has been deleted!')
# Follow these steps
db.reference('path/to/monitor').listen(listener) # It calls the above listener method(It continuosly listens for the data changes in your database)
# Everytime the data changes, listener function will be calledhttps://stackoverflow.com/questions/73836443
复制相似问题