1

Cloud Firestoreはベータ版です。 Googleでは利用可能な情報が少なくなっています。 私は、特定のコレクションに存在する文書の総数をどのように得ることができるのかを知りたいだけです。私たちはforeachループでこれを行うことができますが、これは私が考えるように良い方法ではありません。私はlength機能を試しましたが、機能しません。Cloud FirestoreはIonic 3の文書数を取得します

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) 
    }); 
} 

答えて

1

以下のようにすることができます。

の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; 
    }); 
+0

あなたの迅速な対応に感謝します。今私が上記のコードを実行すると、エラー 'this.afs.collection(...)が返されます。getは関数ではありません。私は私のプロジェクトに 'angularfire2'を使っています。 –

+0

アップデートを参照してください。 – Sampath

+0

これはうまくいきません。その関数は '.map();'の中にないようです。 countはobjcetを返しています。コードが 'c.length'に到達していません。 –

0

この方法を試してみてください:

IMPORT

import { AngularFirestore, AngularFirestoreCollection } from 'angularfire2/firestore'; 

クラス

​​
+0

と同じです。 'console.log'の中には入っていません。 –

関連する問題