有什么方法可以让这个函数同步,或者添加一个回调函数在它完成时运行?
var fbGetLeagues = function(fb) {
var leagues = [];
fb.child('leagues').once('value', function(snapshot) {
snapshot.forEach(function(child) {
var id = child.name();
var name = child.child('name').val();
leagues.push({'id': id, 'name': name});
});
});
return leagues;
};
最佳答案
只需在您的 forEach
之后包含一个回调并运行:
var dbGetLeagues = function(fb, callback) {
snapshot.forEach(function(child) {
var id = child.name();
var name = child.child('name').val();
leagues.push({'id': id, 'name': name});
});
callback(leagues);
}
然后像这样调用代码:
dbGetLeagues(fb, function(leagues) {
console.log(leagues);
});
关于javascript - 如何在 firebase 中获取同步数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21022705/