首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Cloud Firestore获取Ionic 3中的文档数

Cloud Firestore获取Ionic 3中的文档数
EN

Stack Overflow用户
提问于 2017-11-09 14:45:18
回答 3查看 2.1K关注 0票数 1

因为Cloud Firestore处于测试版。谷歌上可用的信息较少。我只想知道如何才能获得特定集合中存在的文档总数。我们可以通过foreach循环来做到这一点,但我认为这不是一个好方法。我尝试了length函数,但它不起作用。

代码语言:javascript
复制
import {AngularFirestore} from 'angularfire2/firestore';

........

constructor(private afs:AngularFirestore) {

     console.log(this.afs.collection(`/cart`).length);  //undefined 

     let ref = this.afs.collection('/cart').valueChanges();
     ref.forEach(element => {
        console.log(element.length);  // total 4 (works fine)
     });
}
EN

回答 3

Stack Overflow用户

发布于 2017-11-09 15:00:26

你可以这样做,如下所示。

使用Javascript API

代码语言:javascript
复制
db.collection(`/cart`).get().then((querySnapshot)=> {      
    console.log(querySnapshot.size); 
});

Angularfire2:

代码语言:javascript
复制
 let count = this.afs.collection(`/cart`).snapshotChanges().map(c => {
      return c.length;
    });
票数 1
EN

Stack Overflow用户

发布于 2017-11-09 22:48:43

尝试这样做:

导入

代码语言:javascript
复制
import { AngularFirestore, AngularFirestoreCollection } from 'angularfire2/firestore';

班级

代码语言:javascript
复制
 cartCollection: AngularFirestoreCollection<any>; //Firestore collection

constructor(private afs:AngularFirestore) {
  this.cartCollection = this.afs.collection('cart');
  this.cartCollection.snapshotChanges().map(data => {
    console.log(data.length);
  });
}
票数 0
EN

Stack Overflow用户

发布于 2018-09-24 08:41:19

Angularfire2

代码语言:javascript
复制
let count = this.afs
    .collection(`/cart`)
    .snapshotChanges()
    .subscribe(c => {
        return c.length;
    });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47195530

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档