尝试列出Firestore数据库的集合 [英] Trying to list collection of Firestore Database

查看:73
本文介绍了尝试列出Firestore数据库的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Ionic4应用程序中列出Firestore数据库的集合,因此我使用

I'd like to list of the collection of Firestore database inside Ionic4 app so I use the doc in the section listCollection so I applied the example code in my code :

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".

And I get this error : error TS2339: Property 'listCollections' does not exist on type 'Firestore'.

我不能使用属性listCollections,但是它位于在线文档中...

I can not use the property listCollections whereas it is in the online doc...

推荐答案

实际上,如Firestore JS SDK中所述

Actually, as detailed in the Firestore JS SDK documentation, retrieving a list of collections IS NOT possible with the mobile/web client libraries.

对于您的Firestore数据库的根集合,也是如此,对于Firestore文档的子集合也是如此.

This is true for the roots collections of your Firestore database but also for the sub-collections of a Firestore document.

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

However, as you have mentioned in your question, it IS possible with the Cloud Firestore Node.js Client API. Therefore you can use a Cloud Function to list the collections of your Firestore DB and call this Cloud Function from your front-end.

由于您将从应用程序中调用此云功能,因此我们使用可调用云功能.

Since you will call this Cloud Function from your app we use a 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,只需遵循Angularfire

Front-end Code

To call this callable Cloud Function from your Angular app, just follow the Angularfire documentation for Cloud Functions.

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文档的所有子集合. (免责声明:我是作者)


Note that this approach is inspired from the following article, which describes how to list all subcollections of a Cloud Firestore document with the JS SDK. (Disclaimer: I'm the author)

这篇关于尝试列出Firestore数据库的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆