将数据推送到Array中无效

时间:2017-06-16 18:22:28

标签: javascript arrays firebase

我正在尝试将推送信息发送到数组,但由于某些原因,某些事情进展不顺利。当我尝试获取数组businessList的长度时,它将导致0,而应该是3.这就是数组在控制台中的样子:

enter image description here

我知道这不是数组在控制台中的样子。似乎推送在我的快照功能中不起作用。我正在使用此代码:

var userId = firebase.auth().currentUser.uid;
var businessesRef = firebase.database().ref("/users/" + userId + "/businesses");

businessesRef.once("value").then(function(snapshot) {

  var businessList = [];

  snapshot.forEach(function(childSnapshot) {

    // key will be "ada" the first time and "alan" the second time
    var key = childSnapshot.key;
    var getListOfBusinesses = firebase.database().ref('/businesses/' + key + "/info");

    getListOfBusinesses.once('value').then(function(snapshot) {
      var name = snapshot.val().name;
      var contact = snapshot.val().contact;
      var pic = snapshot.val().pic;

      $( "#businesses" ).append( '\
        <div class="business">\
          <div class="business-img"><img class="profilepic-m" src="' + pic + '" /></div>\
          <div class="business-arrow"></div>\
          <div class="business-text">\
            ' + name + '<br/>\
            <span class="sidebar-sub">' + contact + '</span>\
          </div>\
        </div>'
      );
      businessList.push(name); 
    });
  });
  console.log(businessList);   
});

0 个答案:

没有答案