简体   繁体   中英

Get Data from Firestore async

I am trying to get data from my Firestore database but i cannot manage to do this properly.

My Problem is that console.log(allData.length) always prints 0 because i guess it overtakes the async part of the code but i cannot figure out to make "userDataSnapshot.docs.map" somehow async aswell. Any tips on how I can do this properly?

      const allData = [];
      const userDataSnapshot = await db.collection("userdata").get();
      userDataSnapshot.docs.map(async (doc) => {
        var userObject = [];
        userObject.push(doc.id);
        var snapshot = await doc.ref.collection("advanceddata").get();
        snapshot.docs.map((doc) => {
          userObject.push(doc.data());
        });
        allData.push(userObject);
      });
      console.log(allData.length);

I believe this will help:

  const allData = [];
  const userDataSnapshot = await db.collection("userdata").get();
  for(const doc of userDataSnapshot.docs) {
   var userObject = [];
    userObject.push(doc.id);
    var snapshot = await doc.ref.collection("advanceddata").get();
    snapshot.docs.map((doc) => {
      userObject.push(doc.data());
    });
    allData.push(userObject);
  }
  console.log(allData.length);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM