Angular Firestore 查询 returns 集合中的所有文档

Angular Firestore query with where returns all docs in collection

我有一个 angular 应用正在使用 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();
}

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

当您在 AngularFirestoreCollection 上调用 docRef.ref 时,它 returns 底层 collection,而不是查询。所以你的 return docRef.ref.get() 确实得到了整个 collection。

我认为您可以使用 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();
}