尝试列出Firestore数据库的集合

n92

我想在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,但它位于在线文档中。

雷诺·塔内克(Renaud Tarnec)

实际上,如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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

获取Cloud Firestore数据库集合

尝试使用 Firestore 数据库从“用户”集合中检索所有文档时出错

如何从 Cloud Firestore 数据库中检索数据集合

使用Angular从Firestore数据库返回多个集合

尝试获取 Firestore 数据库实例时出现 NoClassDefFoundError

如何获取集合>文档>集合>文档中的firestore数据库中的字段?

列出mongo数据库或集合中的所有查询形状

在nodejs脚本中列出mongo数据库中的所有集合

ArangoDB:如何列出或导出数据库中的所有文档(无论集合如何)

Hive数据库仅列出默认数据库

Firestore导入数据库

Flutter Firestore 数据库

尝试从我们的Firestore数据库获取数据字段,但整数返回为null

从flutter监听firestore数据库中的两个集合

是否可以获取 Firebase Cloud Firestore 数据库中可用集合的文档 ID?

Firestore 数据库,查询集合并检查 Python 中是否存在文档

如何将用户的 displayName 添加到 Firestore 数据库集合条目?

如何在Firestore数据库中手动将文档添加到集合?

如何删除firestore集合数据库中的所有文档

如何在Flutter应用程序中检查Firestore数据库集合是否存在文档?

如何将Cloud Firestore数据库集合下载到JSON或CSV文件中?

如何使用where子句删除firestore数据库中集合中的文档?

未列出MSSQLLOCALDB数据库

列出数据库中的表

列出Firebase数据库中的节点

使用SQLAlchemy列出数据库表

使用SQLAlchemy列出数据库表

HeidiSQL不会列出我的数据库

获取 mysql 数据库列以列出