如何在firebase中获取同步数据?

2023-12-24

有什么方法可以使这个函数同步,或者添加一个回调函数以在完成时运行?

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);
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在firebase中获取同步数据? 的相关文章

随机推荐