【发布时间】:2017-11-08 04:16:03
【问题描述】:
我有一个触发两个“价值”Firebase 事件的函数。一个是需要得到children的个数,对应下一个的最深路径。
function myFunction(data){
//get last child
var lastchild = 0;
var data_child = firebase.database().ref('rooms/' + 633 + '/' + 630);
data_child.once('value', function(child) {
if(child.exists()){
lastchild = child.numChildren();
console.log('function 1');
}else{
console.log('error no child');
}
});
//use this number to read the path
var msgsStored = firebase.database().ref('rooms/' + 633 + '/' + 630 + '/' + lastchild);
msgsStored.orderByChild('datetime').once('value', function(snapshot) {
var store = (snapshot.val());
snapshot.forEach(function(child) {
console.log('function 2');
//do something
}
}
}//myFunction
Firebase 始终会在第一个事件之前触发最后一个“值”事件。为什么? 这将始终导致变量 lastchild = 0;并且“功能 2”将始终在控制台上的“功能 1”之前打印。 我也尝试创建一个回调();函数尝试使用 JavaScript 控制顺序,但不起作用。 我知道 Firebase 的不同事件以不同的顺序触发,但就我而言,我只需要读取存储的数据。 有人知道我缺少什么以及如何解决我的问题吗?
【问题讨论】:
标签: javascript firebase