0

I'm trying to go up the hierarchy of my firestore project because its structure is this: Collection -> Documents -> subCollections -> Documents.

I think I'm close to do it but need a little help, I just want for each task that is assigned to the current user to also get the parent document name field and store it in the useState objects array.

import React, { useState, useEffect } from 'react'
import { firestore } from 'firebase/app'
import { db } from '../firebase'
import { useAuth } from '../context/AuthContext'
import { collectionGroup, query, where, getDocs } from "firebase/firestore";  


export default function useFetchTasks() {
  const [loading, setLoading] = useState(true)
  const [error, setError] = useState(null)
  const [tasks, setTasks] = useState([])

  const { currentUser } = useAuth()

  useEffect(() => {
    async function fetchData() {
      try {
        

const projectsTasks = query(collectionGroup(db, 'tasks'), where('assignedTo', '==', currentUser.email));
const querySnapshot = await getDocs(projectsTasks);

querySnapshot.forEach((task) => {

const documentRef = task.ref 
const documentParent = documentRef.parent 



    console.log(documentParent)

    setTasks(tasks => [
        ...tasks,
        {
          title: task.data().title
          /* projectName: get parent document name field here*/ 
                 
        }
      ])

});

        
      } catch (error) {
        setError('Failed to load projects')
        console.error(error)
      } finally {
        setLoading(false)
      }
    }
    fetchData()
  }, [])

  return { loading, error, tasks }
}

André Gomes
  • 397
  • 1
  • 3
  • 17

0 Answers0