基于字段统计Cloud Firestore集合文档数及相关报错求助
Hey there! Let's work through your Firestore document count problems step by step—both the select() type error and that weird "Invalid status code: 19" message.
1. Fixing the "Property 'select' does not exist on type 'CollectionReference'" Error
First off, let's address why your first approach threw a type error.
select() is a valid method for Firestore Query objects, and since CollectionReference inherits from Query, it should work. That error usually pops up for one of these reasons:
- You're using an outdated version of the Firebase SDK (where
select()wasn't properly typed forCollectionReference) - Your TypeScript type dependencies are out of sync (if you're using TypeScript)
Quick Fixes:
- Update your Firebase SDK: Run
npm install firebase@latest(or the equivalent for your package manager) to get the most recent stable version. Newer versions include proper type definitions forselect()on collection references. - Skip
select()entirely (if you only need a count): If your goal is just to count documents,select()doesn't help here—it only reduces data transferred per document, but you're still fetching all document metadata to count the length. For counting, there's a far better method we'll cover below.
2. Resolving the "Invalid status code: 19" Message
Your second approach works for getting the count, but that extra error message is confusing. A few key notes:
- Status code 19 isn't a standard Firestore error—it's likely coming from another part of your code (like a global error handler, an unrelated API call, or a middleware layer).
- Mixing
awaitand.then()syntax can lead to unexpected behavior or unhandled promises that trigger errors elsewhere.
Fix Steps:
First, clean up your code syntax
Stick to either await or .then()—don't mix them. Here's the cleaner await version:
const querySnapshot = await firestore() .collection(collectionName) .where('createdBy', CONDITIONS.EQUALS, reportType) .get(); const documentCount = querySnapshot.size; console.log(documentCount);
Track down the source of status code 19
- Check your codebase for any global error handlers or logging that might be outputting this message. Look for mentions of "status code: 19" or error handling that catches network-related issues.
- If you're running this in a Firebase Cloud Function, check the function logs in the Firebase Console—this might reveal if the error is coming from a network issue or permission misconfiguration.
The Better Approach: Use Firestore's Aggregate count() Query
Instead of fetching all documents just to count them (which is inefficient for large collections), use Firestore's built-in count() aggregation query. It's faster, uses less bandwidth, and avoids potential issues with fetching full document snapshots:
const countSnapshot = await firestore() .collection(collectionName) .where('createdBy', CONDITIONS.EQUALS, reportType) .count() .get(); const documentCount = countSnapshot.data().count; console.log(documentCount);
This method only returns the count, not any document data, making it the optimal choice for your use case.
内容的提问来源于stack exchange,提问作者aka singh

