Yan*_*n92 3 firebase ionic-framework angular google-cloud-firestore
我想列出 Ionic4 应用程序中 Firestore 数据库的集合,因此我使用了listCollection 部分中的文档,因此我在代码中应用了示例代码:
this.afs.firestore.listCollections().then(collections => {
for (let collection of collections) {
console.log(`Found collection with id: ${collection.id}`);
}
});
Run Code Online (Sandbox Code Playgroud)
这是我的构造函数:
constructor(private router: Router,
private afs: AngularFirestore,
private fireauth: AngularFireAuth) { }
Run Code Online (Sandbox Code Playgroud)
我收到此错误:错误 TS2339:“Firestore”类型上不存在属性“listCollections”。
我不能使用属性 listCollections 而它在在线文档中...
实际上,如 Firestore JS SDK文档中所述,使用移动/网络客户端库无法检索集合列表。
这适用于 Firestore 数据库的根集合,也适用于 Firestore 文档的子集合。
但是,正如您在问题中提到的,使用Cloud Firestore Node.js Client API是可能的。因此,您可以使用 Cloud Function列出Firestore 数据库的集合并从前端调用此 Cloud Function。
由于您将从您的应用程序调用此 Cloud Functions 函数,因此我们使用Callable Cloud Functions。
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 };
});
Run Code Online (Sandbox Code Playgroud)
要从您的 Angular 应用程序调用这个可调用的 Cloud Functions,只需按照Cloud Functions的 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({ .... });
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,此方法的灵感来自以下文章,该文章描述了如何使用 JS SDK 列出 Cloud Firestore 文档的所有子集合。(免责声明:我是作者)
归档时间: |
|
查看次数: |
3206 次 |
最近记录: |