Currently, I am facing an issue where I cannot use the result obtained from subscribe() outside of the subscribe function. Whenever I try to console.log the result, it always shows up as undefined. My suspicion is that this might be related to the asynchronous nature of the operation, but I'm unsure of how to resolve this. Would using a promise instead of subscribe() be a better approach in this scenario?
export class MyComponent implements OnInit {
textt: String;
constructor(private tstService: MyComponentService) { this.source = new LocalDataSource(this.data) }
ngOnInit(): void {
this.tstService.getTstWithObservable()
.map(result => result.map(i => i.user.data))
.subscribe(
res => { this.textt = res[0].title; }
);
}
data = [
{
title: this.textt,
sdate: '01/04/1990',
edate: '30/09/1990',
},
];
source: LocalDataSource;
}