0

Prompt: There are 5 products. Each product has 5 video reviews. These reviews are nested in the following data structure.

products > docs > productReviewClips > docs {videoUrl: https://videoclip.mp4, uid: 1234}.

Each productReviewClips doc has a videoUrl.

Am I querying the videoUrls from all the products the wrong way?

const [feed, setFeed] = useState([]);

useEffect(() => { 
        firestore.collection('products').doc().collectionGroup('productReviewClips')
        .onSnapshot((snapshot) => {
            // console.log(JSON.stringify(snapshot) + "1")

            setFeed([...snapshot.docs.map((doc) => {
                return {
                    ...doc.data(),
                    documentID: doc.id,
                };
            })
        ])

        })
        },
   [])
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Mars2024
  • 364
  • 3
  • 15
  • 1
    There's no need for `setFeed([...snapshot.docs.map((doc) => { /* ... */ })]);` as a new array is already generated by `map()`, just use `setFeed(snapshot.docs.map((doc) => { /* ... */ }));` instead. Otherwise, you are effectively doing `setFeed(snapshot.docs.map((doc) => { /* ... */ }).map(docData => docData));` – samthecodingman Nov 07 '21 at 21:59

1 Answers1

2

When you call doc() without arguments, it generates a new unique ID. For a collection group query, skip the initial collection('products').doc() altogether and do:

firestore.collectionGroup('productReviewClips')...

The collectionGroup function is a part of the Firestore object, it's not available on a CollectionReference or DocumentReference object which is also why you get the "not a function" error.

If you want to search only productReviewClips collections under a specific path, that is possible with a trick Sam explains here: CollectionGroupQuery but limit search to subcollections under a particular document

samthecodingman
  • 23,122
  • 4
  • 30
  • 54
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thank you both. I get now --Group is not part of Col or Doc. Really appreciate it, not all heroes wear physical capes, and will archive this for future reference. – Mars2024 Nov 08 '21 at 03:42