【问题标题】:How to call a function if the Firebase data changes using Javascript如果 Firebase 数据使用 Javascript 发生更改,如何调用函数
【发布时间】:2018-05-09 07:38:06
【问题描述】:

当 Firebase 值在 javascript 中发生变化时,我需要调用一个函数, 我尝试在 Javascript 中寻找类似 @​​987654321@ 的函数,但没有找到。

【问题讨论】:

    标签: javascript firebase web firebase-realtime-database


    【解决方案1】:

    使用firebase.database.Reference 的on() 或once() 方法观察事件,详见此处:https://firebase.google.com/docs/database/web/read-and-write#listen_for_value_events

    例如你可以这样做:

    var starCountRef = firebase.database().ref('posts/' + postId + '/starCount');
    starCountRef.on('value', function(snapshot) {
      yourFunction();  //<- call your function here
    });
    

    var starCountRef = firebase.database().ref('posts/' + postId);
    starCountRef.on('value', function(snapshot) {
      yourFunction(snapshot.val().postAuthor);  //<- example, we pass the author of the Post to the function
    });
    

    如果您想将快照中的值作为函数的参数传递

    【讨论】:

      猜你喜欢
      • 2020-10-26
      • 1970-01-01
      • 1970-01-01
      • 2021-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-20
      • 1970-01-01
      相关资源
      最近更新 更多