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

400
Views
Firebase v9 orderBy error: failed to fetch data TypeError: Cannot read properties of undefined (reading 'startAt')

I am new to Firebase v9 and I cannot seem to make the orderBy work, but when I tried in the Firebase console, I can see the results if I were to use the orderBy

enter image description here

error:

Failed to fetch data TypeError: Cannot read properties of undefined (reading 'startAt')

useEffect(() => {
  const retrieve = async () => {
    const querySnapshot = await getDocs(collection(db, "orders"));
    const q = query(querySnapshot, orderBy("orderCreatedAt"));
    console.log(q, "q");
    const arr = [];
    querySnapshot.forEach((doc) => {
      arr.push({
        ...doc.data(),
        id: doc.id,
      });
    });
    if (isMounted) {
      setOrders(arr);
    }
  };
}, []);

in the firebase console:

enter image description here

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

0

The query() functions accepts a Query instance for the first parameter. This can be provided by the CollectionReference returned by collection().

Your issue is that you are passing it the result of getDocs() which resolves with a QuerySnapshot.

I think you need something like this

const ordersRef = collection(db, "orders");
const q = query(ordersRef, orderBy("orderCreatedAt");
const querySnapshot = await getDocs(q);

See also

  • Perform simple and compound queries in Cloud Firestore
  • Order and limit data with Cloud Firestore

I would highly recommend using typescript to avoid these sorts of issues. It would have immediately told you that your original querySnapshot wasn't the right type for use in query().

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!