Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

137
Views
Firebase onSnapshot collection and subcollection merge with useEffect hook

hey I have data looks like this in my Firestore db

  • folders
    • randomkeyGenerator - has fields like the name of folder the icon etc...
      • subcollection calls Tasks - it has a lot of documents and each documents have values like title etc...

what I want to do is get all folders for specific user and it subcollection which the result should look like this

data = [{folderId: "randomkeyGenerator" , folderTitle: "work", folderIcon: "๐Ÿ‘๐Ÿป", Tasks: [{},{},{},{}]}, {other folder etc....}]

here is my code with useEffect hook I did everything right but still it shows empty array or sometimes it shows only folder fields without Tasks or empty array in tasks here is the result

Array [
  Object {
    "Tasks": Array [],
    "folderIcon": "๐Ÿ˜จ",
    "folderId": "3QwC21noAwmvUAVXhJfs",
    "folderTitle": "Work",
  },
  Object {
    "Tasks": Array [],
    "folderIcon": "๐Ÿคต๐Ÿฟ",
    "folderId": "ewRYhuI1TGxL1EhGq5ZB",
    "folderTitle": "Untitled",
  },
]

here is my code:

  useEffect(() => {
    setLoading(true);
    const subscriber = taskRef
      .where("userID", "==", user.userId)
      .orderBy("createdAt")
      .onSnapshot((querySnapshot) => {
        const queryData = [];
        querySnapshot.forEach((documentSnapshot) => {
          const queryObject = {
            folderId: documentSnapshot.id,
            folderIcon: documentSnapshot.data().Icon,
            folderTitle: documentSnapshot.data().name,
            Tasks: [],
          };
          documentSnapshot.ref
            .collection("Tasks")
            .where("userID", "==", user.userId)
            .onSnapshot((querySnapshotTasks) => {
              querySnapshotTasks.forEach((documentSnapshotTask) => {
                queryObject.Tasks.push({
                  ...documentSnapshotTask.data(),
                  key: documentSnapshotTask.id,
                });
              });
            });
          queryData.push(queryObject);
        });
        console.log(queryData);
        setData(queryData);
        setLoading(false);
      });
    return () => subscriber();
  }, []);
about 4 years ago ยท Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!