检索现有数据后,Firebase运行脚本 [英] Firebase run script after retrieving existing data

查看:112
本文介绍了检索现有数据后,Firebase运行脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  firebase.database()。在js中有一个函数需要刷新。 ref('/ users /'+ this.userId +'/info').on('child_added',function(snapshot){
this.myFunction();
} .bind(this)) ;

问题是,Firebase中的数据非常大,所以我不需要运行该功能很多次。这是我如何运行:
$ b


  1. 从Firebase检索所有现有数据。

  2. 运行这个函数(一次)。
  3. 每次从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:

    1. Retrieve ALL EXISTING data from Firebase.
    2. Run the function (once).
    3. 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 with child_ events. You can use the guarantee that the value event will fire after all the related child_ 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆