P粉2657249302023-08-25 19:54:35
In fact, as documented in the Firestore JS SDK documentation , the collection list cannot be retrieved using the mobile/web client libraries.
This applies not only to the root collection of the Firestore database, but also to subcollections of Firestore documents.
However, as you mentioned in your question, it is possible to use the Cloud Firestore Node.js client API. Therefore, you can use a Cloud Function to list a collection of Firestore databases and call this cloud function from the frontend.
Since you will be calling this cloud function from your app, we use callable cloud function .
const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(); exports.getCollections = functions.https.onCall(async (data, context) => { const collections = await admin.firestore().listCollections(); const collectionIds = collections.map(col => col.id); return { collections: collectionIds }; });
To call this callable cloud function from your Angular application, just follow the Angularfire Cloud Functions documentation.
import { Component } from '@angular/core'; import { AngularFireFunctions } from '@angular/fire/functions'; @Component({ selector: 'app-root', template: `{ data$ | async }` }) export class AppComponent { constructor(private fns: AngularFireFunctions) { const callable = fns.httpsCallable('getCollections'); this.data$ = callable({ .... }); } }
Note that this method is inspired by the following article on how to list all subcollections of a Cloud Firestore document using the JS SDK. (Disclaimer: I am the author)