In my Angular service I am providing a public Observable called usageDoc$
.
service.ts:
usageDoc$: Observable<IUsage>;
initializeUsageDoc() {
//gets called in app.component.ts on app load
...
//some async db querying
this.usageDoc$ = this.firestore.getUsageDoc(user.uid); //getUsageDoc(..) returns Observable<IUsage>
}
component.ts
localDoc: any;
ngOnInit() {
this.service.usageDoc$.subscribe(doc=>this.localDoc=doc);
}
This leads to the following error: cannot read property subscribe of undefined...
as usageDoc$
is not yet set on the component init. Currently I am using a workaround by creating a second observable usageDocReady$ = new Subject<boolean>
in service that emits as soon as usageDoc$
is set.
Is there better to solve this issue? Can I somehow initialize usageDoc$
with a default value?
I know I would not have this issue if I'd subscribe in template using the async pipe, but I need a normal variable for displaying matters, hence the use of localDoc
.