如何使For循环等待Firebase数据库响应? [英] How to make For loop wait for Firebase Database response?

查看:93
本文介绍了如何使For循环等待Firebase数据库响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个接收对象数组的函数。数组如下所示:

I have a function that receives an object array. The array looks like this:

var receivedObjects = [{nuih329hs: 100}, {8suwhd73h: 500}, {2dsd73ud: 50}, {9u238ds: 200}];

每个键都是一个ID,每个值都是一个数字。
对象按固定顺序排列,我要做的是遍历对象并将每个值定义为变量,然后在创建html行时将使用该变量。

Each key is an ID, and each value is a number. The objects are in a set order and what I want to do is iterate through the objects and define each value as a variable, which I'll then use when creating a html row.

我遇到的问题是,我必须在此迭代代码内调用Firebase数据库,其结果是,为该行成功创建了一行每个对象值,输入到每一行的值始终相同(对象数组中的最后一个/最近值),因此在上述情况下,出现数字 200 在所有四行中。

The problem I'm having is, I have to make a call to a Firebase database inside this iteration code, the result is that while a row is successfully being created for each object value, the value entered into each row is always the same (the last/most recent value in the object array), so in the above case the number 200 is appearing in all four rows.

我认为这可能是因为可能所有迭代都在第一个Firebase调用完成之前完成,这意味着变量 currentValue (我要输入到行中)在进行首次Firebase调用之前设置为数组中的最后一个值。

I think this might be happening because maybe all the iterations are completing before the first Firebase call is even completed, meaning the variable currentValue (which I'm entering into the rows) is set at the last value in the array before the first Firebase call is made.

注意:还有一个包含Firebase ID的数组(称为 listOfIds ),我能够成功地使用此数组中的每个ID作为变量在Firebase调用中使用, var currentID 是该变量。

Note: There is another array (called listOfIds) which contains Firebase IDs, I'm able to successfully use each ID from this array as a variable to use in the Firebase call, var currentID is that variable.

这是函数:

function populateTable(receivedObjects){

   var receivedObjectsLength = receivedObjects.length;
   // Note: an object array called listOfIds exists here (containing Firebase IDs to be used for querying Firebase), it's referenced below.

   for (var i = 0; i < receivedObjectsLength; i++) {

      // listOfIds is an Object array 
      var currentID = Object.keys(listOfIds[i]);

      var term = (Object.keys(receivedObjects[i])[0]);
      var currentValue = receivedObjects[i][term];

      //The following line is showing the correct ID and Value for each iteration:
      console.log("The current ID is: "+currentID+" and the current value is: "+currentValue);

      firebase.database().ref('/users/' + currentID).once('value').then(function(child) {

      // This is where the rows are created, ``currentValue`` is used here, but it's appearing as the same value in every row.

      // The next line is showing the correct current ID, but it's showing the currentValue as "200" in every row.
      console.log("The current ID is: "+currentID+" and the current value is: "+currentValue);

    });
  }
}

我很困惑,因为我以为Javascript迭代代码将等待,直到从Firebase调用返回数据为止,但是似乎不是这样吗?我如何更改代码,以使 console.log(当前ID为: + currentID +,当前值为: + currentValue); Firebase调用成功函数将显示正确的 currentValue

I'm very confused, because I thought the Javascript iteration code would wait until the data gets returned from the Firebase call, but it seems this isn't the case? How can I change my code so that the line console.log("The current ID is: "+currentID+" and the current value is: "+currentValue); inside the Firebase call success function will show the correct currentValue?

推荐答案

让循环等待,您可以使用函数将 currentID currentValue 放入自己的作用域,以使它们不会t每次迭代都会被覆盖:

Instead of making the loop wait, you can use a function to put currentID and currentValue into their own scope so that they don't get overwritten with each iteration:

function getUser(currentID, currentValue) {
    return firebase.database().ref('/users/' + currentID).once('value').then(function(child) {
        console.log("The current ID is: "+currentID+" and the current value is: "+currentValue);
    });
}

在您的循环中:

for (var i = 0; i < receivedObjectsLength; i++) {
    ...
    getUser(currentID, currentValue)
}

这篇关于如何使For循环等待Firebase数据库响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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