Advertisement
marichan022

Firebase recursive getDocWithSubcollections

Aug 24th, 2021
2,678
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export const getDocWithSubcollections = async (doc: any) => {
  2.   const item: any = { id: doc.id, ...doc.data() };
  3.   const subcollections = await doc.ref.listCollections();
  4.   for (const sub of subcollections) {
  5.     const arr = [];
  6.     const subDocs = (await sub.get()).docs;
  7.     for (const d of subDocs) {
  8.       arr.push(await getDocWithSubcollections(d));
  9.     }
  10.     item[sub.id] = arr;
  11.   }
  12.   return item;
  13. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement