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
asyn
c 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:
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,
};
});
}