1

I have an angular app that is using Firestore. Whenever I query for docs in collection that meet a specific condition, the array that is returned contains every document in the collection. I do not understand why this is happening as I am following the documentation.

On call to the collection in the component

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 service making the call

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

Below is a screen shot of the versions collection. It shows one of the returned docs, which does not even have the required field.

enter image description here

1 Answer 1

2

When you call docRef.ref on a AngularFirestoreCollection it returns the underlying collection, not the query. So your return docRef.ref.get() is indeed getting the entire collection.

I think you can use docRef.query to get the query, but I don't even thing there's any reason to use an AngularFire call at all here. Since your code is already using the plain JavaScript API to process the documents, you might as well stick to that SDK in your getDocsByParam too:

getDocsByParam( collection, getParam:string, paramValue:string ) {
    var docRef = this.afs.collection(collection).ref;
    return docRef.where(getParam, '==', paramValue).get();
}
Sign up to request clarification or add additional context in comments.

7 Comments

What is the "ref" in the above snippet? How do I get the data from that return?
What is the Angular method of getting this data? Is it to use switchmap?
Sorry about that, I keep forgetting how AngularFire's query syntax works. I updated my answer to show how to do this while using AngularFire to build the reference to the collection. If you want to use full AngularFire syntax, you can do that too. But the code you shared that calls getDocsByParam(...) uses regular JavaScript syntax, which is why I answered with that.
Thanks. This did work, but I am frustrated with the fact that I felt like I followed the documentation as well as possible and I am having great difficulty with the angular firestore docs. They are very confusing to me on a number of levels.
Sorry about Joshua. If you have an idea how to improve them, the project is open-source and typically quite welcomes contributions.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.