Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 数组未在firebase once()内更新_Javascript_Arrays_Angularjs_Firebase - Fatal编程技术网

Javascript 数组未在firebase once()内更新

Javascript 数组未在firebase once()内更新,javascript,arrays,angularjs,firebase,Javascript,Arrays,Angularjs,Firebase,我在firebaseref.once()函数之外声明了一个数组customers var客户=[] 我正在更改ref.once()中数组的值,并尝试从ref.once()中访问修改后的值。但它返回初始值[] 这是我的密码 var customers = []; var nameRef = new Firebase(FBURL+'/customerNames/'); nameRef.once("value",function(snap){ customers.push("t

我在firebase
ref.once()函数之外声明了一个数组
customers

var客户=[]

我正在更改
ref.once()
中数组的值,并尝试从
ref.once()
中访问修改后的值。但它返回初始值
[]

这是我的密码

  var customers = [];
  var nameRef = new Firebase(FBURL+'/customerNames/');
  nameRef.once("value",function(snap){
      customers.push("test");
  });
  console.log(customers); // returns []

问题在于
once
回调是异步执行的,而log语句实际上是在
customers.push(“test”)之前调用的。请尝试以下代码以查看代码的执行顺序:

var customers = [];
var nameRef = new Firebase(FBURL+'/customerNames/');
nameRef.once("value",function(snap){
    customers.push("test");
    console.log("Inside of callback: " + customers); // returns [test]

    // At this point, you can call another function that uses the new value.
    // For example:
    countCustomers();
});
console.log("Outside of callback: " + customers); // returns []

function countCustomers() {
    console.log("Number of customers: " + customers.length);
}

但是,我想在数组
customers
中使用
once()之外的推送值。如何实现?在执行回调之前,该值不会更改。因此,您应该在回调中调用需要修改值的适当代码。您可能应该更好地解释您需要什么,和/或发布需要修改值的ode,以获得更具体的解释。我如何知道回调已完成执行?这样我就可以调用另一个使用数组值的代码了?