I have a service, that returns Observables<SubscriberModel[]>
.
export interface SubscriberModel {
email: string;
}
in my component I get subscribers$ in constructor:
public subscribers$: Observable<SubscriberModel[]>;
this.subscribers$ = this.subscribersService.getAll();
and show their in template:
<p *ngFor="let subscriber of subscribers$ | async">{{subscriber.email}}</p>
<button (click)="onDownloadClick()">
<a [href]="fileUrl" download="file.txt">DownloadFile</a>
</button>
I have to download my subscribers into file:
public data: any;
public onDownloadClick(): void {
this.subscribers$.pipe(
map(res => res.map(i => i.email)),
)
.subscribe(res => {
console.log(res) //array of string
this.data = res;
});
console.log(this.data); //undefined
const blob: Blob =
new Blob([this.data], {type: 'application/octet-stream'});
this.fileUrl =
this.sanitizer.bypassSecurityTrustResourceUrl(window.URL.createObjectURL(blob));
}
Why in console.log this.data
is undefined?