I’m working with Firebase Firestore and have created a collection using user IDs as document IDs. However, when I try to query the loginUser
collection to retrieve data, it shows zero documents. On the other hand, when I query another collection (where the document IDs were generated automatically by Firestore), everything works fine, and the documents are retrieved as expected.
Issue:
-
I manually created documents in the
loginUser
collection, using the user’s UID as the document ID. -
When I attempt to fetch the documents from this collection, it shows “No documents found”, even though the documents exist in the Firebase Console.
-
Querying a different collection (where document IDs were auto-generated by Firestore) works perfectly fine.
Single Document Query works correctly
var querySnapshot = await FirebaseFirestore.instance
.collection("loginUser")
.doc(user.uid)
.get();
but when I retrieve all doc it shows zero
static Future<int> getUserCount() async {
try {
// Fetch the collection snapshot
QuerySnapshot querySnapshot =
await FirebaseFirestore.instance.collection('loginUser').get();
if (querySnapshot.docs.isNotEmpty) {
print("Documents found:");
querySnapshot.docs.forEach((doc) {
print("Document ID: ${doc.id}");
});
} else {
print("No documents found in the 'loginUser' collection.");
}
return querySnapshot.size;
} catch (e) {
print("Error fetching user count: $e");
return 0; // Return 0 if an error occurs
}
}
firebase Rule
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth != null;
}
}
}
Why isn’t my query retrieving documents from the loginUser
collection, even though they exist in Firestore? Could this be related to how I’m using user IDs as document IDs, or am I missing something else?
1