我想在Ionic4应用程序中列出Firestore数据库的集合,因此我在listCollection部分中使用了该文档,因此我在代码中应用了示例代码:
this.afs.firestore.listCollections().then(collections => {
for (let collection of collections) {
console.log(`Found collection with id: ${collection.id}`);
}
});
这是我的构造函数:
constructor(private router: Router,
private afs: AngularFirestore,
private fireauth: AngularFireAuth) { }
我收到此错误:错误TS2339:类型“ Firestore”上不存在属性“ listCollections”。
我不能使用属性listCollections,但它位于在线文档中。
实际上,如Firestore JS SDK文档中所述,使用移动/网络客户端库无法检索集合列表。
对于您的Firestore数据库的root集合,也是如此,对于Firestore文档的子集合也是如此。
然而,正如你在你的问题中提到,它IS可以与云公司的FireStore Node.js的客户端API。因此,您可以使用Cloud Function列出Firestore DB的集合,并从前端调用此Cloud Function。
由于您将从应用程序中调用此Cloud Function,因此我们使用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 };
});
要从您的Angular应用中调用此可调用的Cloud Function,只需遵循有关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({ .... });
}
}
请注意,此方法的灵感来自以下文章,该文章描述了如何使用JS SDK列出Cloud Firestore文档的所有子集合。(免责声明:我是作者)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句