I try to get data from my server. Inside my subscribe function I have data but when i try to use it outside function console.log return undefined
Service:
getPostDetail():Observable<Post> {
return this.http.get<Post>(this.detailPostUrl);
}
Component:
export class PostDetailComponent implements OnInit {
post: Post;
constructor(private postsService:PostsService) {
this.postsService.getPostDetail().subscribe(data => {
this.post = data;
console.log(this.post);
});
}
ngOnInit(): void {
console.log(this.post); // undefined
}
}
I see that first I get a undefined from ngOnInit's console.log() but I don't know how to change it.
I can add that I have very similar function and that one works.
Anyone can help ?