如何在Firebase中获得同步数据? [英] How to get synchonous data in firebase?

查看:144
本文介绍了如何在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中获得同步数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆