【发布时间】:2018-05-09 07:38:06
【问题描述】:
当 Firebase 值在 javascript 中发生变化时,我需要调用一个函数, 我尝试在 Javascript 中寻找类似 @987654321@ 的函数,但没有找到。
【问题讨论】:
标签: javascript firebase web firebase-realtime-database
当 Firebase 值在 javascript 中发生变化时,我需要调用一个函数, 我尝试在 Javascript 中寻找类似 @987654321@ 的函数,但没有找到。
【问题讨论】:
标签: javascript firebase web firebase-realtime-database
使用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
});
如果您想将快照中的值作为函数的参数传递
【讨论】: