首页 > 解决方案 > AngularFire collectionGroup 总是抛出错误不是一个有效的函数

问题描述

我正在使用 Ionic 4,并且刚刚与 AngularFire 集成。集合和文档运行良好,但是我无法使 collectionGroup 函数正常工作 - 它总是给出该函数不存在的错误。

相关代码是:


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

我已将名称中带有“Fire”的所有模块更新为最新版本:@angular/fire @ionic-native/firebase-x angularfire firebase firebase-admin

但错误仍然出现。此外,如果我尝试使用 .collection() 函数查询食谱集合,它只会返回一个空结果..即使“食谱”集合下有文档

标签: javascriptfirebasegoogle-cloud-firestoreionic4angularfire

解决方案


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).


推荐阅读