In the below class when i try to console.log the datas property after completing subscription it printing undefined why is that ? Though it is populated by the service ?
export class AppComponent {
datas: any;
constructor(private service: SpacexServiceService) {
this.service.get_data().
subscribe(res => this.datas = this.flatten(res),
error => console.log('Error happened' + error),
function () {
console.log(this.datas);
});
}
flatten(obj) {
const result = {};
for (const i in obj) {
if (typeof obj[i] === 'object') {
const flatobj = this.flatten(obj[i]);
for (const x in flatobj) {
result[i + '_' + x] = flatobj[x];
}
} else {
result[i] = obj[i];
}
}
return result;
}
}