带自定义日期字符串ID的Firestore文档无法获取问题咨询
Hey there, let's work through why your Firestore query is returning zero documents even though you know there are 3 in the Bookings By Date collection. Here are the key areas to check step by step:
1. Verify the Current User's UID Matches Firestore
Your query starts with FirebaseAuth.getInstance().getUid() to target the user's document in the Users collection. First, make sure this UID is exactly what's in your Firestore console:
- Add a log to print the UID:
Log.d(TAG, "Current logged-in user UID: " + FirebaseAuth.getInstance().getUid()); - Head to your Firestore console, check the
Userscollection, and confirm the document ID matches the logged-in user's UID exactly (Firestore IDs are case-sensitive!).
2. Double-Check the Collection Name
The collection name Bookings By Date has spaces and specific capitalization—Firestore is strict about both. If your console shows the collection as bookings by date, Bookings by Date, or any other variation, the query won't find it.
- Go directly to the Firestore console and compare the collection name to what's in your code. Even a single wrong space or capital letter will break the query.
3. Check Your Firestore Security Rules
This is a super common gotcha: if your security rules don't allow the logged-in user to read the Bookings By Date collection, Firestore will silently return an empty result (no error in Logcat unless you explicitly check for failures).
- For debugging, temporarily set your rules to test mode (don't leave this in production!):
rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { match /{document=**} { allow read, write: if true; } } } - If the query returns documents after this, your original rules were too restrictive. Update them to only let users access their own bookings, like this:
match /Users/{userId}/Bookings By Date/{bookingDate} { allow read, write: if request.auth.uid == userId; }
4. Add Failure Handling to Your Query
Your current code only handles successful queries, but if there's an error (like permission denied or network issues), you won't see it. Add a failure log to catch these:
.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() { @Override public void onComplete(@NonNull Task<QuerySnapshot> task) { if (task.isSuccessful()) { // Your existing success code here Log.d(TAG, "onComplete: called"); Log.d(TAG, "onComplete: task result = " + task.getResult()); Log.d(TAG, "onComplete: document count = " + task.getResult().getDocuments().size()); } else { // Critical: Log the error to see what's wrong Log.e(TAG, "Failed to load bookings", task.getException()); } } });
This will print any exceptions to Logcat, which can point you straight to the issue.
5. Validate Your Date String Document IDs
You're using date strings as document IDs—make sure those IDs in Firestore match the format you expect, and don't include invalid characters. Firestore doesn't allow /, \, #, [, ], or * in document IDs.
- Check the
Bookings By Datecollection in your console: are the IDs formatted like2024-05-20,20240520, or something else? Ensure there are no typos or invalid characters here.
6. Confirm Firebase Initialization
If your google-services.json file is incorrect or linked to the wrong Firebase project, your app might be connecting to a different Firestore database entirely.
- Double-check that your
google-services.jsonmatches the project you're viewing in the Firebase console, and that the project ID in the file matches the one in the console.
Start with these checks—one of them is almost certainly the cause of your empty result!
内容的提问来源于stack exchange,提问作者Ssubrat Rrudra

