Observable Undefinded In Facade Architeture With Rxjs
(Following the suggestion so gunnar, i'm editing my question) @Gunnar.B Service for connection with api @Injectable({ providedIn: 'root' }) export class ConsolidadoApi { constr
Solution 1:
listInvestments method is performing an asynchronous API call. So basically the investments property still have undefined when you called forEach on it.
You want to loop in investments array after you load data from the server not before.
One way to do it is to make sure you are inside subscribe's next() callback and there you are certain you have the data you need to perform your action.
CoreService class becomes:
@Injectable({
providedIn: 'root'
})
exportclassCoreService {
publicinvestments: any;
constructor(private api: ConsolidadoApi, private state: StateService) {}
publiccreateMenu(){
this.api.getInvestments()
.subscribe(response => {
this.investments = response;
this.investments.forEach(element => {
console.log(element)
});
})
}
}
Update on question edit
In ngOnInit you called loadInvestments() that returns an observable. And observable are lazy, that means if you didn't call subscribe on them nothing will happen.
What you need to do is change this line:
this.coreService.loadInvestments();
to
this.coreService.loadInvestments().subscribe();
Post a Comment for "Observable Undefinded In Facade Architeture With Rxjs"