Jacques Schalkwyk
Jacques Schalkwyk

Reputation: 59

How to query subcollections in AngularFire2/FireStore?

I am trying to query subcollections in AngularFire2 with FireStore with no success. 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:

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

I am a newbie at this.

Upvotes: 0

Views: 1851

Answers (1)

Remi
Remi

Reputation: 742

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

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.

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

Upvotes: 1

Related Questions