简体   繁体   English

如何比较值和从Firebase实时数据库中删除?

[英]How to compare value and delete from firebase realtime database?

I am using Firebase realtime database in Android app, and have data like this: 我在Android应用中使用Firebase实时数据库,并且具有如下数据:

这是我的数据库

How can I access that data from javascript. 如何从javascript访问该数据。 I just wanna code function to get value of every endTime and compare with current time and if endTime <= Current Time then delete values yellow marked on anther image. 我只是想对函数进行编码以获取每个endTime的值并与当前时间进行比较,如果endTime <= Current Time,则删除花药图像上标记为黄色的值。

黄色标记值

Events is fixed value. 事件是固定值。

Indjija, Vojvodina and -KsB2mVkpgQe_fRyFFH4 are auto generated so it's possible to have more values like that two. Indjija,Vojvodina和-KsB2mVkpgQe_fRyFFH4是自动生成的,因此可能有更多类似这两个值。

I know how to solve that in android studio with Java code but I'm interesting about implementing server functions. 我知道如何使用Java代码在android studio中解决该问题,但是我对实现服务器功能很感兴趣。

The answer to this question is buried deep within Firebase Database Reference . 这个问题的答案深藏在《 Firebase 数据库参考》中 forEach() is used to get to the child without knowing the child's exact path. forEach()用于在不知道孩子确切路径的情况下到达孩子。

var leadsRef = database.ref('leads');
leadsRef.on('value', function(snapshot) {
    snapshot.forEach(function(childSnapshot) {
      var childData = childSnapshot.val();
    });
});

Now childSnapshot will contain the required data, the same thing can also be accessed using child_added . 现在childSnapshot将包含所需的数据,也可以使用child_added访问相同的child_added

leadsRef.on('child_added', function(snapshot) {
      //Do something with the data
});

The only difference is that in case of forEach() , it will loop through from the start so if any new data will be added, it will also load the previous data but in case of child_added , the listener is only on new child and not the previous existing childs. 唯一的区别是,在forEach()情况下,它将从头开始循环遍历,因此,如果将添加任何新数据,它还将加载先前的数据,但是在child_added情况下,侦听器仅在新的子对象上而不是在以前的现有孩子。

Useful Link: 有用的链接:

Firebase Read and Write Web Firebase读写网站

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM