因为Cloud Firestore处于测试版。谷歌上可用的信息较少。我只想知道如何才能获得特定集合中存在的文档总数。我们可以通过foreach循环来做到这一点,但我认为这不是一个好方法。我尝试了length函数,但它不起作用。
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)
});
}发布于 2017-11-09 15:00:26
你可以这样做,如下所示。
使用Javascript API
db.collection(`/cart`).get().then((querySnapshot)=> {
console.log(querySnapshot.size);
});Angularfire2:
let count = this.afs.collection(`/cart`).snapshotChanges().map(c => {
return c.length;
});发布于 2017-11-09 22:48:43
尝试这样做:
导入
import { AngularFirestore, AngularFirestoreCollection } from 'angularfire2/firestore';班级
cartCollection: AngularFirestoreCollection<any>; //Firestore collection
constructor(private afs:AngularFirestore) {
this.cartCollection = this.afs.collection('cart');
this.cartCollection.snapshotChanges().map(data => {
console.log(data.length);
});
}发布于 2018-09-24 08:41:19
Angularfire2
let count = this.afs
.collection(`/cart`)
.snapshotChanges()
.subscribe(c => {
return c.length;
});https://stackoverflow.com/questions/47195530
复制相似问题