How to get synchonous data in firebase?

后端 未结 1 1762
清歌不尽
清歌不尽 2021-02-10 15:32

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 = [         


        
相关标签:
1条回答
  • 2021-02-10 15:53

    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);
    });
    
    0 讨论(0)
提交回复
热议问题