Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
So this is my code :

JavaScript
let newTems = {}
let IDs = ['xyz', 'abc']
let temRef = await db.collection("templates")

For each id in IDs, I am checking if the id is equal to the documentID of any document in the "templates" collection and mapping name along with id to the newTems.

What I have tried:

JavaScript
IDs.forEach((id, i) => {
  let temSnap = temRef.where(admin.firestore.FieldPath.documentId(), '==', id).get()
  
  temSnap.forEach(doc => {
    let temData = doc.data()
    
    newTems[i] = {
      id: temData.doc.id,
      name: temData.name,
    }
  })
})

I am getting an error saying

error
TypeError: temSnap.forEach is not a function

I have tried looking for any syntactical errors but could not find any. Why is this happening?

Thanks for your help.
Posted
Updated 4-May-21 11:16am

1 solution

I had two issues here. The first one is that I called the get() without awaiting it for the tempSnap and because that is a async call I can't use it in a forEach because forEach doesn't support async.

To resolve the problem I first loop through the IDs with a for loop because that iterator supports async calls. And as second used the await on the get() call.

The final code looked like this:

JavaScript
let newTems = {}
let IDs = ['xyz', 'abc']
let temRef = await db.collection("templates")

for (let i = 0; i < IDs.length; i++) {
  const id = IDs[i];
  let temSnap = await temRef
    .where(admin.firestore.FieldPath.documentId(), "==", id)
    .get();

  temSnap.forEach((doc) => {
    let temData = doc.data();

    newTems[i] = {
      id: temData.doc.id,
      name: temData.name,
    };
  });
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900