I am trying to get the information of a specific loan by it's ID and the details of items in that loan to display it in the detail page. However, I am unable to and I do not know how to retrieve the loan and loan items using the loan service's getLoanById method given to me.
detail.page.ts
export class DetailPage {
loan: Loan;
loanId: string;
constructor(private route: ActivatedRoute, private loanService: LoanService) {
this.loanId = this.route.snapshot.params.id;
this.loan = this.loanService.getLoanById(this.loanId);
}
}
loan.service.ts
getLoanById(id: string) {
return firebase.firestore().collection('loans').doc(id).get().then(doc => {
let loan = new Loan(doc.data().username, doc.data().status, doc.data().duedate.toDate(), doc.id);
return firebase.firestore().collection('loans/' + id + '/items').get().then(collection => {
loan.items = []; // Empty array
collection.forEach(doc => {
let item = new Item(doc.id, doc.data().quantity);
loan.items.push(item);
})
return loan;
});
});
}