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

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

问题描述

我想列出 Ionic4 应用程序中 Firestore 数据库的集合,所以我使用 doc 在部分 listCollection 中,所以我在我的代码中应用了示例代码:

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.因此,您可以使用云函数来列出您的 Firestore 数据库的集合并从您的前端调用此云函数.

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 应用程序调用这个可调用的云函数,只需按照 Angularfire 文档 适用于 Cloud Functions.

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天全站免登陆