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,
};
})
])
})
},
[])
CodePudding user response:
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