简体   繁体   English

如何在AngularFire2 / FireStore中查询子集合?

[英]How to query subcollections in AngularFire2/FireStore?

I am trying to query subcollections in AngularFire2 with FireStore with no success. 我正在尝试使用FireStore查询AngularFire2中的子集合,但没有成功。 Have searched but found no suitable solution. 搜索过,但没有找到合适的解决方案。

ngOnInit() {
// this does work
this.africaCollection = this.afs.collection('Country', ref => ref
.where('code', '==', 'za'));
this.countriesAfrica = this.africaCollection.valueChanges();

// this does not work
this.townCollection = this.afs.collection('Country').doc('za')                         
.collection('Towns');
this.towns = this.townCollection.valueChanges();
{

My HTML looks like this: 我的HTML看起来像这样:

<div>
  <table>
    <tr *ngFor="let town of towns | async">
      <td>{{ town.name }}</td>
    </tr>
  </table>
</div>

I am a newbie at this. 我是这个新手。

It may be related to your rules. 这可能与您的规则有关。

Default rules 默认规则

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write;
    }
  }
}

More details about rules here: 有关规则的更多详细信息,请参见:

https://firebase.google.com/docs/firestore/security/secure-data#rules_dont_cascade https://firebase.google.com/docs/firestore/security/secure-data#rules_dont_cascade

Make sure you have items in that collection 确保您有该收藏中的物品

You can also add items to your collection by code to be sure it's correctly located. 您还可以通过代码将项目添加到集合中,以确保其位置正确。 You'll then be able to look at your firebase console to see where it's been placed. 然后,您可以查看您的Firebase控制台,查看其放置位置。

var town: Town = {
  name: "town name",
  region: "region name"
};
this.afs.collection('Country').doc('za').collection('Towns').add(town);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM