Reputation: 15303
I have a Observable from I am subscribing the data, If I am not set the setTimeout
I am not able to get thechildnode
of the object. at present I set the timeout. but looking for correct approach of angular.
here is my code :
ngOnInit() {
this.store.appData.subscribe(value => {
this.appData = value;
setTimeout(() => this.setLocales(), 1000); //requires this
} );
}
setLocales(){
console.log( this.appData.hdr.country );
}
error I am getting:
ERROR TypeError: Cannot read property 'country' of undefined
at HeaderComponent.setLocales (header.component.ts:32)
at SafeSubscriber.eval [as _next] (header.component.ts:26)
at SafeSubscriber.__tryOrSetError (Subscriber.js:252)
at SafeSubscriber.next (Subscriber.js:192)
at Subscriber._next (Subscriber.js:131)
at Subscriber.next (Subscriber.js:95)
at BehaviorSubject._subscribe (BehaviorSubject.js:30)
at BehaviorSubject.Observable._trySubscribe (Observable.js:172)
at BehaviorSubject.Subject._trySubscribe (Subject.js:98)
at BehaviorSubject.Observable.subscribe (Observable.js:160)
ERROR CONTEXT DebugContext_ {view: {…}, nodeIndex: 17, nodeDef: {…}, elDef: {…}, elView: {…}}
Upvotes: 0
Views: 1447
Reputation: 3858
give a type any for value or provide corresponding interface and provide a check inside the subscribe function.
ngOnInit() {
this.store.appData.subscribe((value: any) => {
if(value && value.hdr) {
this.appData = value;
this.setLocales();
}
} );
}
Hope this works.
Upvotes: 1