I'm trying to get a single document from a firestore collection that I can reference in the component's view template (like {{invoice.id}}.
I can get the document data as an Observable, but can't get get the values to show when I try to reference this in the template.
view-invoice.component.html:
<h4 class="page-title">Invoice Summary</h4>
<h5>Invoice ID: {{ (invoice.id | async).id }}</h5>
view-invoice.component.ts:
import { Component, OnInit, Input } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import { AngularFireDatabase } from 'angularfire2/database';
import { AngularFirestore, AngularFirestoreCollection, AngularFirestoreDocument } from 'angularfire2/firestore';
import { AuthService } from '../../services/auth.service';
import { Invoice } from '../invoiceModel';import 'rxjs/add/operator/mergeMap';
@Component({
selector: 'app-view-invoice',
templateUrl: './view-invoice.component.html',
styleUrls: ['./view-invoice.component.scss']
})
export class ViewInvoiceComponent implements OnInit {
userId: string;
invoiceId: any;
invoicesCollection: AngularFirestoreCollection<Invoice>;
invoices: Observable<Invoice[]>;
invoice: Observable<{}>;
constructor(private authService: AuthService, private db: AngularFirestore, private route: ActivatedRoute) {
this.userId = this.authService.user.uid;
this.route.params.subscribe(params => {
this.invoiceId = params.id;
})
this.invoicesCollection = this.db.collection('/invoices');
this.invoices = this.invoicesCollection.snapshotChanges().map(changes => {
return changes.map(a => {
const data = a.payload.doc.data() as Invoice;
data.id = a.payload.doc.id;
return data;
})
})
}
ngOnInit() {
this.getInvoice();
console.log('this.invoice: ');
console.log(this.invoice);
}
getInvoice() {
this.invoice = this.db.collection('/users').doc(this.userId).collection('/invoices', ref => ref.where('invoiceId', '==', this.invoiceId).limit(1)).valueChanges().flatMap(result => result);
}
}