Reputation: 1
Firebase function return ob2 before data is fetched from firestore via loop
return admin.firestore().collection("doctors").doc(data.type).listCollections().then(function (doc) {
// console.log(doc);
var arr=[];
let ob2;
for(let coll of doc) {
ob2=admin.firestore().collection("doctors").doc(data.type).collection(coll.id).doc("docdetail").get().
then((x)=>{
arr.push(x.data());
obj.users=arr;
return obj;
})
}
console.log(ob2);
return ob2;
})
Upvotes: 0
Views: 66
Reputation: 18555
You're working with a document reference.
An example is posted online:
let documentRef = firestore.doc('col/doc');
documentRef.listCollections().then(collections => {
for (let collection of collections) {
console.log(`Found subcollection with id: ${collection.id}`);
}
});
Upvotes: 0
Reputation: 520
Your issue here is you aren't using promises properly. your code at the bottom "return ob2" is executed immediately before anything is returned. Modify your code to the below code and it should hopefully work. Or better yet use async await.
return admin.firestore().collection("doctors").doc(data.type).listCollections().then(function (doc) {
// console.log(doc);
var arr=[];
let ob2;
for(let coll of doc) {
return admin.firestore().collection("doctors").doc(data.type).collection(coll.id).doc("docdetail").get().
then((x)=>{
arr.push(x.data());
obj.users=arr;
console.log(obj);
return obj;
})
};
});
Upvotes: 1