React native无法在循环中从Firebase设置多个ArrayItem

React native无法在循环中从Firebase设置多个ArrayItem,firebase,react-native,asynchronous,Firebase,React Native,Asynchronous,我试图从循环中的Firebaserealtime数据库获取数据并设置数组项, 但只有最后一项可以设置。 看起来像是synchronize问题我尝试了很多方法,但都无法解决 import FireBaseConnection from '../classes/firebaseconnection.js'; const getComments = () => { let cardatafetch=[] FireBaseConnection.GetData('/PostC

我试图从循环中的
Firebase
realtime数据库获取数据并设置数组项, 但只有最后一项可以设置。 看起来像是
synchronize
问题我尝试了很多方法,但都无法解决

import FireBaseConnection from '../classes/firebaseconnection.js';

 const  getComments = () => {
    let cardatafetch=[]
     FireBaseConnection.GetData('/PostComments/1234').then((comments) => {
        for (i in comments) {
            cardatafetch.push(comment[i]) 
        }

 for (j in cardatafetch) { 
     var UserId =  cardatafetch[j]["UserID"]   

  FireBaseConnection.GetData('/Users/'+UserId).then((user) => {

     cardatafetch[j].ProfilePicture=user["ProfilePicture"] 
          })
        .catch((error) => {
           console.log(error)
              });
 }
       console.log(cardatafetch)

           }).catch((error) => {
           console.log(error)
              });

 }
    } 
控制台输出为

在从存储器获取图像的过程中也存在同样的问题

   for (j in cardatafetch) { 
  FireBaseConnection.GetImage().then((obj) => {
     cardatafetch[j].ProfilePicture=obj
          })
             .catch((error) => {
           console.log(error)
              }); 
 }

FireBaseConnection类

import database from '@react-native-firebase/database';
import storage from '@react-native-firebase/storage';
import { utils } from '@react-native-firebase/app';

 class FireBaseConnection
 {
  static async  GetData(refValue) {
        let data;
 await database()
  .ref(refValue)
  .once('value')
  .then(snapshot => {
    data =  snapshot.val();
  });
  return data;
}

static async  GetImage(imgValue) {
 const reference = storage().ref(imgValue);
 let imagePath= await reference.getDownloadURL().then(result =>result);
return imagePath;
}
}
export default FireBaseConnection;

尝试下面的代码,我所做的是将代码放在循环的最后一次迭代中,这样当所有项都被推送到数组中时,它将只实现一次

import FireBaseConnection from '../classes/firebaseconnection.js';

const getComments = () => {
    return new Promise((resolve, reject) => {
        let commentsArr = [];
        FireBaseConnection.GetData('/PostComments/1234').then((comments) => {
            Object.keys(comments).forEach((key, index) => {
                commentsArr.push(comments[key])

                if(index == Object.keys(comments).length-1) {
                    resolve(commentsArr);
                }
            });
        }).catch((error) => {
            console.log(error)
        });
    });
}

const addImagesToComment = () => {
    this.getComments().then((comments) => {
        var finalArr = [];
        comments.forEach((comment, index) => {
            var tempComment = comment;
            var UserId = comment["UserID"]
            FireBaseConnection.GetData('/Users/' + UserId).then((user) => {
                tempComment.ProfilePicture = user["ProfilePicture"]
                finalArr.push(tempComment);
            }).catch((error) => {
                console.log(error)
            });

            if(index == comments.length-1) {
                console.log(finalArr)
            }
        });
    });
}

尝试调用getComments函数。

comments.length不适用于json结果。我尝试了const commentLength=Object.keys(comments).length for length对于length来说是可以的,但结果仍然相同。只是最后一项