I've been working on an Angular4 application.
Here is the BookingService I'm utilizing to fetch the data-
export class BookingService {
constructor(private http: HttpClient) {}
getMemberBookings(memberID: number): Observable<any> {
return this.http.get('http://myapi.com/bookings/member/'+memberID).map(response => response['bookings']);
}
}
Then in my component-
export class PrintButtonComponent implements OnInit {
bookings: any;
constructor(private service: BookingService) {}
ngOnInit() {}
downloadPDF() {
this.retrieveBookings(memberID);
//pdf creation logic
}
retrieveBookings(memberID: number) {
this.service.getMemberBookings(memberID).subscribe(data => this.bookings = data);
}
}
The challenge arises when attempting to use the service's data in the downloadPDF method, as it contains other necessary information for PDF creation. However, when the data is returned from the subscribe or assigned to a property, it appears as undefined. It is clear that this issue stems from the asynchronous nature of observables, but integrating the pdf creation logic within the subscribe method is not ideal. As a newcomer to Angular and observables, I am seeking guidance on how to address this dilemma. Thank you.