Home > Software design >  Firebase admin only works with forEach?
Firebase admin only works with forEach?

Time:10-17

I have this code:

admin
  .firestore()
  .collection("Orders")
  .where("branch_id", "==", elemA.data()["branch_id"])
  .where("create_on", ">=", startOfToday)
  .where("create_on", "<=", endOfToday)
  .orderBy("create_on", "desc")
  .get()
  .then(async (response) => {});

And trying to loop over the response inside.

This worked using a forEach:

response.forEach((el) => {
   console.log(el.data());
});

Though it did not work using a regular for-loop nor a with a for-of:

for(let i = 0; i < response.size; i  = 1) {
   const el = response[i];
   console.log(el.data());
}
for (const el of response) {
   console.log(el.data());
}

Why is this happening? Is it a limitation from firebase-admin ?

I'm switching away from forEach to properly use await inside.

CodePudding user response:

Response is a generic object (an instance of QuerySnapshot), not an array. To get an array, call response.docs.

for(let i of response.docs) {
   console.log(i.data());
}
  • Related