Reputation: 123
Im new to firebase. I want to retrieve all the data via userId then sort the createdAt field to desc order but it doesnt work for me.
Any help is greatly appreciated.
useEffect(() => {
const user = JSON.parse(localStorage.getItem('user'));
const q = query(collectionRef, where('userId', '==',user.uid),orderBy('createdAt','desc'));
onSnapshot(q, (snapshot) => {
const documents = snapshot.docs.map((doc) => {
return {
...doc.data(),
doc_id: doc.id
};
});
setIsLoading(false);
setTodos(documents);
});
}, []);
Upvotes: 0
Views: 372
Reputation: 83068
Your problem most probably comes from the fact that Firestore misses the composite index corresponding to your query.
Since you didn't add an error callback in your onSnapshot()
method call, you cannot see the error. Adding a callback will firstly allow you to confirm that it is a problem of missing composite index and, secondly, show you an URL (in the error message) that will allow you to automatically build this index.
Upvotes: 1