Trying to get a discord bot to join a channel and play a link but I get this error
TypeError: Cannot read properties of undefined (reading 'videos')
my command handler is fine with other simpler commands
const videoFinder = async (query) => {
const videoResult = await ytSearch(query);
return (videoResult.videos.length > 1) ? videoResult.videos[0] : null;
}
This is the line that gets me the error
You may try something like this :
const videoFinder = async (query) => {
const videoResult = await ytSearch(query);
return (videoResult?.videos?.length > 1) ? videoResult?.videos[0] : null;
}
The ? will check the object before exists. If not, it returns undefined and in your case, null, because undefined.videos doesn't exist.
Alternatively :
const videoFinder = async (query) => {
const videoResult = await ytSearch(query);
if (!videoResult || !videoResult.videos) return null
return videoResult.videos[0]
}