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
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:
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
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().