
I am making a quiz application with React. There is a collection of questions in my category collection. I can access categories. How can I access this collection of questions? What should I add to my code?
const fetchData = async () => {
const catRef = collection(db, "categories");
const querySnapshot = await getDocs(catRef);
querySnapshot.forEach((doc) => {
console.log(doc.id, " => ", doc.data());
//const questionRef = collection(db, `categories/${doc.id}/questions`);
});
};
The collection(db, 'categories/${categoryId}/questions'); approach is correct if you are trying to get questions of a specific category.
If you are trying to fetch questions from all categories then you can use a collectionGroup() query instead:
import { collectionGroup, query, where, getDocs } from "firebase/firestore";
const questions = query(collectionGroup(db, 'questions'));
const querySnapshot = await getDocs(museums);
querySnapshot.forEach((doc) => {
console.log(doc.id, ' => ', doc.data());
});