0

In my Cloud Firestore database, my top collection is called meters, all the documents have custom numeric ids like 12345, and each document has a subcollection called meterdata and all the document ids for each of those are dates like 2019-12-14.

In my meter-list.component.ts I have

export class MeterListComponent implements OnInit {

 meters: Observable<any[]>;

 constructor(db: AngularFirestore) {
    this.meters = db.collection(('meters'), ref=> ref.where("usagetoday", ">", 0)).valueChanges();
  }

and in my meter-list.component.html I have a loop

<div *ngFor="let meter of meters | async">
{{meter.id}}
{{meter.address}}
{{meter.usagetoday}}
{{meter.lastupdated}}
</div>

which returns a list of all the meters, with the address and a field which is updated daily with the latest usage.

I would like to be able to reference in the meter-list.component.html a calculated field called morning on each day's meterdata document, but I'm not sure how to reference the subdocument.

I know that it is not {{meter[meter.id].['meterdata'][meter.lastupdated].morning}}.

I also tried to include a line in the meter-list.component.ts that would look like this.meterdata = db.collection('meters').doc(this.meters.id).collection('meterdata').doc(this.meters.lastupdated).valueChanges(); which would allow me to call *ngIf = "meterdata | async as meterdata" inside the *ngFor loop and include {{ meterdata.morning }}, but that obviously doesn't work either.

I'd really appreciate any push in the right direction. Thank you!

Ezra Butler
  • 120
  • 2
  • 13
  • Can you paste the structure of the `meters` json in the question? – Nicholas K Jan 02 '20 at 06:09
  • I think what you are looking for is this: "meters" - "12345" - "meterdata" - "2019-12-14". The way I would directly refer to this would be collection('meters').doc('12345').collection('meterdata').doc('2019-12-14') – Ezra Butler Jan 02 '20 at 16:24
  • Err, not really. What would `meter` look after the constructor is invoked? – Nicholas K Jan 02 '20 at 16:28
  • you mean this? (this is the `doc.data()` of a `meter`.) ```{ address: '123 Dumbledore Dr', city: 'Beverly Hills', day: 'tuesday', hourlyarray: [ 251, 36, 26, 24, 119, 47, 8, 18, 32, 76, 206, 72, 72, 99, 50, 94, 144, 64, 155, 140, 166, 94, 90, 68 ], lastupdated: '2019-12-31', meter: '12345', perunit: 33.61, state: 'CA', units: 64, zipcode: '90210' }``` – Ezra Butler Jan 02 '20 at 17:03
  • Yes. Now with this json what is it that you are trying to display? – Nicholas K Jan 02 '20 at 17:06
  • I'm using Cloud Firestore. These are only the fields on this particular document. This document has a subcollection called `meterdata` and that subcollection has a document called `2019-12-14` whose json looks like ```{ date: '2019-12-14', day: 'saturday', earlymorning: 612, evening: 1284, hourlyarray: [ 165, 76, 34, 311, 26, 88, 291, 333, 413, 498, 323, 153, 375, 250, 289, 284, 259, 247, 300, 194, 297, 327, 140, 240 ], meter: '12345', midday: 1390, morning: 1623, night: 1004, perunit: 140.78, units: 64, usage: 5913 } ``` – Ezra Butler Jan 02 '20 at 17:13
  • And I would like to display the `earlymorning` from that document. – Ezra Butler Jan 02 '20 at 17:14
  • Well I'm not too sure about cloud firestore, but can't you store the required json into a variable and then work with that? – Nicholas K Jan 02 '20 at 17:15
  • I theoretically can. The problem is that I'm trying to access other information as well, and this was the easiest possible way to pose the question to be understandable. And once I start storing all the relevant fields on the original document it gets to be rather unwieldy. – Ezra Butler Jan 02 '20 at 17:20
  • 1
    Hmm, that's true also. I wish you luck then! – Nicholas K Jan 02 '20 at 17:24
  • I think you can check this link https://stackoverflow.com/questions/46573014/firestore-query-subcollections – Harif Velarde Jan 18 '20 at 00:22

0 Answers0