Angular Firestore查询,其中where返回集合中的所有文档

时间:2020-06-02 07:54:15

标签: javascript firebase google-cloud-firestore angularfire2

我有一个使用Firestore的角度应用程序。每当我查询集合中满足特定条件的文档时,返回的数组都会包含集合中的每个文档。我不明白为什么following the documentation时会发生这种情况。

在调用组件中的集合

this.FirebaseService.getDocsByParam( 'versions', 'projectId', this.projectData.uid )
    .then((snapshot) => {
        var tempArray = [];
        var docData;
        snapshot.forEach((doc) => {
            docData=doc.data();
            docData.uid=doc.id;
            tempArray.push(docData);
        });
        this.versionList = tempArray;
        this.versionData = this.versionList[this.versionList.length-1];
        this.initializeAll();
    })
    .catch((err) => {
      console.log('Error getting documents', err);
});

Firebase服务进行呼叫

getDocsByParam( collection, getParam:string, paramValue:string ) {
    var docRef = this.afs.collection(collection, ref => ref.where(getParam, '==', paramValue));
    return docRef.ref.get();
}

下面是版本集合的屏幕截图。它显示了返回的文档之一,甚至没有必填字段。

enter image description here

1 个答案:

答案 0 :(得分:0)

docRef.ref上调用AngularFirestoreCollection时,它将返回基础集合,而不是查询。因此,您的return docRef.ref.get()实际上正在获取整个收藏集。

我认为您可以使用docRef.query来获取查询,但是我什至没有任何理由在这里完全使用AngularFire调用。由于您的代码已经使用普通的JavaScript API来处理文档,因此您最好也坚持使用getDocsByParam中的SDK:

getDocsByParam( collection, getParam:string, paramValue:string ) {
    var docRef = this.afs.collection(collection).ref;
    return docRef.where(getParam, '==', paramValue).get();
}
相关问题