I have a method with an observable and the data retrieved from it is processed after in another method. The clasificacionController.listCatalogo returns an observable. This is my code:
getCatalogs(node: CatalogBean): CatalogBean[] {
let children;
const filterBean = {
inicio: 0,
maximo: 0,
direccion: '',
sort: undefined,
id: node.idcatalogo,
} as FilterBean;
this.clasificacionController.listCatalogo(filterBean).subscribe(catalogs => {
console.log('getChildren Catalogs', catalogs);
children = catalogs;
});
return children;
}
Then I use this method to do something else:
toggleNode(node: FlatNode, expand: boolean) {
const children = this.database.getChildren(node.catalogo); //THIS IS WERE I USE THE METHOD ABOVE
const index = this.data.indexOf(node);
if (!node.catalogo.hijos || index < 0) { // If no children, or cannot find the node, no op
return;
}
if (expand) {
node.isLoading = true;
//THEN I PROCESS THE DATA HERE
const nodes = children.map(catalogo =>
new FlatNode(catalogo, node.level + 1, this.database.isExpandable(catalogo)));
console.log('Data antes del corte', this.data);
this.data.splice(index, 0, ...nodes);
console.log('Data despues del corte', this.data);
// notify the change
this.dataChange.next(this.data);
node.isLoading = false;
} else {
this.data.splice(index + 1, children.length);
this.dataChange.next(this.data);
}
}
But the map method trows an error (undefined) because the children variable has no data. There is way to wait for the getChildren method to finish and then use the map. Thanks in advance