如何在Firebase中获得同步数据

如何在Firebase中获得同步数据

本文介绍了如何在Firebase中获得同步数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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});
});
});
回归联赛;
};


解决方案

只要包含一个回调&如果你的 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});
});
回调(联赛);
}

然后调用代码:

  dbGetLeagues(fb,function(leagues){
console.log(leagues);
});


Is there any way I can make this function synchronous, or add a callback function to run when it's finished?

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;
};
解决方案

Just include a callback & Run if after your 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);
}

And call the code like:

dbGetLeagues(fb, function(leagues) {
    console.log(leagues);
});

这篇关于如何在Firebase中获得同步数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 22:59