Having some trouble with a piece of code that subscribes from our service:
Method getVideo()
getVideo() {
this.watchService.getVideoByVideoString(this.videoId).subscribe(
(Response) => {
this.videoData = Response;
console.log(Response);
},
(err) => console.log(err)
);
}
ngOnInit Method
ngOnInit() {
this.getVideo();
console.log(this.videoData);
}
Unfortunately, when I try to access this.videoData, it shows up as undefined. Can someone assist me with this issue?
Thank you.
======
Updated
After running some tests, I found out that I need to manipulate the data within the function itself instead of in the NgOnInit
.
The final solution looks like this:
getVideo() {
this.watchService.getVideoByVideoString(this.videoId).subscribe(
(Response) => {
this.videoData = Response;
if (this.videoData.title == null) {
this.titleService.setTitle('InternalVideoChannel');
} else {
this.titleService.setTitle('InternalVideoChannel - ' + this.videoData.title);
}
if (this.videoData.id == 0) {
console.log('Video doesnt exist on the database');
this.router.navigate(['/notFound']);
}
console.log(Response);
},
(err) => console.log(err)
);
}
Thanks!