I am trying to filter and retrieve specific items based on the categoryID using the "where" method and the "==" operator.
In my Firestore collection named "task", each user has a document with an array of different tasks. Here is the structure: Click here for image
This is how I add tasks to the task collection for a user:
const addToFirebase = async() => {
if(dataFetch) {
await setDoc(doc(db, "task", `${firebase.auth().currentUser.uid}`), {
tasks: tasks
}, {merge: true});
}
}
I have attempted to create a query to retrieve filtered tasks, but it is not working as expected:
const getFilteredTasks = async() => {
const collectionRef = collection(db, "task", `${firebase.auth().currentUser.uid}`, "tasks");
const q = query(collectionRef, where("categoryID", "==", item.id));
console.log('outside snapshot')
console.log(item.id)
const querySnapshot = await getDocs(q);
querySnapshot.forEach((doc) => {
console.log(doc.data());
console.log('inside snapshot')
});
}
When I call the above function, it logs "outside snapshot" and the correct item.id, which is the categoryID for each task, but it does not log the doc.data() and "inside snapshot".