Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

239
Views
Make query with "where" filter in Firestore

I want to filter and get specific items based on the categoryID, where i use the "where" method and the "==" operator.

I have this Firestore collection called "task", where the documents are divided by userID and each document for a user contains an array of different tasks. The structur is seen here: Enter image description here

This is how i add the array tasks for an user in the task collection:

const addToFirebase = async() => {
    if(dataFetch) {
      await setDoc(doc(db, "task", `${firebase.auth().currentUser.uid}`), {
        tasks: tasks
      }, {merge: true});
    }
  }

And this is how i have tried to make my query that is not working:

 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 the above function is called, it logs the "outside snapshot" and the correct item.id, which is the categoryID for each task, but it does not log the doc.data() and the "inside snapshot".

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You've set up your query as though each task will be a separate document in a collection, but based on the screenshot and the way you're setting the data, you just have a single document per user. That document contains all the individual tasks. So to access that you would get the document and use client side code to use the parts of the data you care about:

const getFilteredTasks = async () => {
  const docRef = doc(db, 'task', `${firebase.auth().currentUser.uid}`);
  const snapshot = await getDoc(docRef);
  const data = snapshot.data();
  if (!data) {
    return [];
  } else {
    return data.tasks.filter(task => task.categoryID === item.id);
  } 
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!