Search code examples
javascriptfirebasegoogle-cloud-firestoreionic4angularfire

AngularFire collectionGroup always throws error not a valid function


I'm using Ionic 4 and have just integrated with AngularFire. Collections and documents work well, however I cannot get the collectionGroup function to work - it always gives an error that the function does not exist.

Relevant code is :


this.user = this.firestore.collection('profile').doc(tok.uid);



 async StoreRecipe(recipe_name,meal) {

     var ua = await this.read_foods_meal(meal+1)
     console.log(meal);
     ua.subscribe( foods => { console.log(foods);
        foods.forEach(food =>  { this.user.collection('recipe').doc(recipe_name).collection('foods').add(food);} )
     });

 }
 
 async read_recipes() {
     
     var ua = await this.user.collectionGroup('recipe');
     return(ua);
 }

I have updated all modules with 'Fire' in the name to the latest version : @angular/fire @ionic-native/firebase-x angularfire firebase firebase-admin

But the error still appears. Also, If I try to query the recipe collection using the .collection() function it just returns a null result .. even though there are documents under the 'recipe'collection


Solution

  • You user object is an instance of the firebase.firestore.DocumentReference class, and it does not have a collectionGroup method. Check the documetation here.

    If you want to run a query across various collections, these should have the same name, and you can use the firebase.firestore().collectionGroup() method. You can find how to configure it in this link.

    If you just want to get all the documents inside your recipe collection, you can make use of the CollectionReference.get() method (documentation).