Your Firebase comments are failing because Firestore requires composite indexes for complex queries with multiple where clauses and orderBy clauses.
FirebaseError: The query requires an index. That index is currently building and cannot be used yet.
Firestore needs indexes to efficiently query documents when you combine:
- Multiple
whereconditions orderByclauses- Different field combinations
I've temporarily simplified the queries by:
- Removing
orderBy('createdAt', 'desc')from main comments query - Removing
orderBy('createdAt', 'asc')from replies query - Sorting in memory instead (JavaScript sorting)
This allows comments to work immediately while indexes build.
- Visit https://console.firebase.google.com/
- Select your project:
agritech-blog-comments - Click "Firestore Database" in the left sidebar
- Click the "Indexes" tab at the top
- You should see "Composite" indexes section
- Collection ID:
comments - Fields to index:
postId(Ascending)parentId(Ascending)isApproved(Ascending)createdAt(Descending)
- Collection ID:
comments - Fields to index:
postId(Ascending)parentId(Ascending)isApproved(Ascending)createdAt(Ascending)
-
Click "Create Index"
-
Collection ID:
comments -
Add fields one by one:
postId→ AscendingparentId→ AscendingisApproved→ AscendingcreatedAt→ Descending (for main comments)
-
Click "Create"
-
Create second index for replies:
postId→ AscendingparentId→ AscendingisApproved→ AscendingcreatedAt→ Ascending (for replies)
- Indexes show status: "Building" → "Ready"
- Usually takes 1-5 minutes for small datasets
- You'll see a green checkmark when ready
The error message includes a direct link to check index status:
https://console.firebase.google.com/v1/r/project/agritech-blog-comments/fir…RoMCghwYXJlbnRJZBABGgoKBnBvc3RJZBABGg0KCWNyZWF0ZVRBdBACGgwKCF9fbmFtZV9fEAI
Click this link to go directly to the index status page.
| Status | Meaning | Action |
|---|---|---|
| Building | Index is being created | Wait 1-5 minutes |
| Ready | Index is active | ✅ Good to go! |
| Error | Index creation failed | Check field types |
| Missing | Index doesn't exist | Create manually |
Once indexes are ready, you can re-enable the optimized queries by uncommenting the orderBy clauses in comment-service.ts:
// Re-enable these after indexes are ready:
orderBy('createdAt', 'desc') // For main comments
orderBy('createdAt', 'asc') // For replies- Always create indexes before using complex queries
- Use ascending/descending consistently across indexes
- Test queries in Firebase Console first
- Monitor index usage in Firebase Console
- Ensure
createdAtis a timestamp field - Ensure
postIdandparentIdare strings - Ensure
isApprovedis a boolean
- Check if fields exist in your documents
- Verify field names match exactly
- Ensure you have write permissions
- Wait longer for indexes to build
- Check if you're using the correct collection name
- Verify your Firebase project ID
- Immediate fix: ✅ Applied (simplified queries)
- Comments: ✅ Working with in-memory sorting
- Next step: Create Firestore indexes
- Final goal: Re-enable optimized queries
- Go to Firebase Console → Indexes tab
- Create the two composite indexes listed above
- Wait for indexes to build (1-5 minutes)
- Test your comments - they should work immediately
- Re-enable optimized queries once indexes are ready
Your comments system is working now with the simplified queries, but will be much faster once the proper indexes are in place! 🚀