检索现有数据后,Firebase运行脚本 [英] Firebase run script after retrieving existing data
问题描述
firebase.database()。在js中有一个函数需要刷新。 ref('/ users /'+ this.userId +'/info').on('child_added',function(snapshot){
this.myFunction();
} .bind(this)) ;
问题是,Firebase中的数据非常大,所以我不需要运行该功能很多次。这是我如何运行:
$ b
- 从Firebase检索所有现有数据。
- 运行这个函数(一次)。
- 每次从Firebase获得更多数据时,再次运行该函数。
这是可能的吗?
谢谢。
解决方案Yevgen的答案是这样做的一个方法。但这意味着你将在每次更改时处理所有的孩子,这可能不是你想要的。
另一种方法是使用
value
事件,然后随后使用child _
事件进行更改。你可以使用保证,在所有相关的child _
事件之后,值
事件将触发,以检测初始有效载荷是
$ $ $ $ $ $ $ $ $ $ $ $
$ {
snapshot.forEach(function(child){
console.log('用+ child.key做某事);
});
isInitialValueDone = true;
});
ref.on('child_added',function(snapshot){
if(isInitialValueDone){
console.log('用+ snapshot.key做某事);
}
});
I have a function in js that needs to refresh after I retrieve data from Firebase.
firebase.database().ref('/users/' + this.userId + '/info').on('child_added', function(snapshot){ this.myFunction(); }.bind(this));
The problem is that the data in Firebase is very large so I don't what to run the function lots of times. This is how I what it to run:
- Retrieve ALL EXISTING data from Firebase.
- Run the function (once).
- Every time more data comes in from Firebase, run the function again.
Is this possible?
Thanks.
解决方案Yevgen's answer is one way to do this. But it means you'll be processing all children on each change, which may not be what you want.
An alternative is to process the initial children using a
value
event and then the subsequent changes withchild_
events. You can use the guarantee that thevalue
event will fire after all the relatedchild_
events to detect whether the initial payload is done.var isInitialValueDone = false ref.once('value', function(snapshot) { snapshot.forEach(function(child) { console.log('Do something with '+child.key); }); isInitialValueDone = true; }); ref.on('child_added', function(snapshot) { if (isInitialValueDone) { console.log('Do something with '+snapshot.key); } });
这篇关于检索现有数据后,Firebase运行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!