Ich versuche, eine Sammlung von Firestore-Datenbanken aufzulisten
P粉546179835
P粉546179835 2023-08-24 23:19:00
0
1
513
<p>Ich möchte eine Firestore-Datenbanksammlung in einer Ionic4-Anwendung auflisten, daher verwende ich das Dokument aus dem Abschnitt „listCollection“ und habe den Beispielcode in meinen Code übernommen: </p> <pre class="brush:php;toolbar:false;">this.afs.firestore.listCollections().then(collections => { for (lassen Sie eine Sammlung von Sammlungen) { console.log(`Sammlung mit der ID gefunden: ${collection.id}`); } });</pre> <p>Das ist mein Konstruktor: </p> <pre class="brush:php;toolbar:false;">constructor(private router: Router, private Afs: AngularFirestore, private fireauth: AngularFireAuth) { }</pre> <p>Ich erhalte diesen Fehler: Fehler TS2339: Die Eigenschaft „listCollections“ ist für den Typ „Firestore“ nicht vorhanden. </p> <p>Ich kann das Attribut listCollections nicht verwenden, da es in der Online-Dokumentation enthalten ist... </p>
P粉546179835
P粉546179835

Antworte allen(1)
P粉265724930

实际上,如 Firestore JS SDK 文档 中所述,使用移动/网络客户端库无法检索集合列表

这不仅适用于 Firestore 数据库的根集合,也适用于 Firestore 文档的子集合。

但是,正如您在问题中提到的,可以使用 Cloud Firestore Node.js 客户端 API。因此,您可以使用 Cloud Function 来列出 Firestore 数据库的集合并从前端调用此云函数。

由于您将从应用中调用此云函数,因此我们使用可调用云函数 .

云函数代码

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 };

});

前端代码

要从 Angular 应用程序调用此可调用云函数,只需遵循 Angularfire 云函数文档

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({ .... });
  }
}

请注意,此方法的灵感来自以下文章,介绍如何使用 JS SDK 列出 Cloud Firestore 文档的所有子集合。 (免责声明:我是作者)

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!