SuspiciousLookingOwl / youtubei

Get Youtube data such as videos, playlists, channels, video information & comments, related videos, up next video, and more!
https://suspiciouslookingowl.github.io/youtubei
MIT License
240 stars 50 forks source link

Set default value for result to avoid throwing error #52

Closed musso closed 2 years ago

musso commented 2 years ago

When using await youtube.getChannel("UCni1rOH6_7JL8Nm4QtZvFXw") to crawl a channel without thumbnail, it throws error TypeError: Cannot read property 'thumbnails' of undefined. Better to check if it is undefined or null before accessing it. Not all channel got thumbnail or join date etc

musso commented 2 years ago

Possible fix, In src/classes/Channel.ts, line 60

this.banner = new Thumbnails().load(banner.thumbnails);
this.tvBanner = new Thumbnails().load(tvBanner.thumbnails);
this.mobileBanner = new Thumbnails().load(mobileBanner.thumbnails);

change to

this.banner = new Thumbnails().load(banner?.thumbnails || []);
this.tvBanner = new Thumbnails().load(tvBanner?.thumbnails || []);
this.mobileBanner = new Thumbnails().load(mobileBanner?.thumbnails || []);
musso commented 2 years ago

I found that there are many similar cases existed in the library .... many of them throwing error due to accessing undefined object. Wonder if you have time to fix them or you could accept pull requests

SuspiciousLookingOwl commented 2 years ago

Most of these type of bugs are easy to fix and doesn't take too much time, finding out which one causes the bug is the one that takes time. I haven't tested my library thoroughly, with every possible scenario.

I'm accepting pull requests, but you can just send your bug report here and I can fix all of them quite quickly, and should be published within days.

SuspiciousLookingOwl commented 2 years ago

This should be fixed in v1, but if the issue still persists, fill free to reopen the issue.

(Also check the v1 migration guide)