使用node-firestore-backup-restore备份Firestore仅读取单个文档求助
Hey Dave, sorry to hear you're stuck with this odd issue—since your iOS app can access all the documents without a problem, the root cause is almost certainly tied to how the backup script interacts with Firestore. Here are some actionable steps to debug and fix this:
Double-Check Firebase Admin Service Account Permissions
Verify the service account your script uses. If it’s assigned a restricted custom role (instead of a broad one likeCloud Datastore UserorEditor), it might only have access to that single document. Even if you think permissions are correct, reassigning the role and retrying can rule out IAM propagation delays that might be blocking access to other docs.Inspect the Script’s Query Logic
Dig into thenode-firestore-backup-restoresource code, specifically the section that fetches collection documents. Look for:- Accidental filtering: Is there a hardcoded
where()clause that’s unintentionally limiting results to one document? - Document existence checks: Some scripts mark docs as "non-existent" if they don’t have top-level fields (even if they have subcollections). If your missing docs only contain subcollections and no root fields, the script might be incorrectly skipping them.
- Pagination gaps: Firestore returns docs in batches (max 1000 per request). If the script doesn’t handle pagination with cursors, it might stop after the first batch—but this is less likely if you’re only getting one document total.
- Accidental filtering: Is there a hardcoded
Add Verbose Logging to Catch Hidden Errors
Modify the script to log detailed info when fetching each document. For example, in the document retrieval section, add:docRef.get().then(snapshot => { if (!snapshot.exists) { console.warn(`Document ${docRef.id} marked as missing — snapshot details:`, snapshot); } else { // Process the document as usual } }).catch(err => { console.error(`Failed to fetch document ${docRef.id}:`, err); });This might reveal hidden issues like permission denials for specific docs or unexpected data types the script can’t handle.
Update Dependencies for Compatibility
Outdated versions offirebase-admin(the SDK the script relies on) can have bugs with newer Firestore features (like timestamp fields, special characters in document IDs, or nested subcollections). Try updating thefirebase-adminentry in the script’spackage.jsonto the latest stable version, then runnpm installand retry the backup.Test Individual Document Fetches
Write a tiny standalone script to fetch one of the "missing" documents directly using the same service account. For example:const admin = require('firebase-admin'); const serviceAccount = require('./your-service-account-key.json'); admin.initializeApp({ credential: admin.credential.cert(serviceAccount) }); const db = admin.firestore(); db.collection('your-target-collection').doc('problem-document-id').get() .then(snapshot => { console.log('Snapshot exists:', snapshot.exists); console.log('Document data:', snapshot.data()); }) .catch(err => console.error('Fetch error:', err));If this script can fetch the document, the issue is definitely in the
node-firestore-backup-restorecode’s logic. If it can’t, you’ll get a clear error message pointing to the root cause (like invalid permissions or malformed document paths).
内容的提问来源于stack exchange,提问作者David Mrozinski

