res.tracks[0] is *null*

Hello, I am encountering this issue right now: If i search a song using the title and author, i get an array of tracks from my search result. However if I search something using a spotify track link, I get loadType "track" and res.tracks[0] is null
8 Replies
afraid-scarlet
afraid-scarlet•2y ago
your code?
rival-black
rival-blackOP•2y ago
https://sourceb.in/78FjLwQcGn i fixed it by adding this at the top:
if (!res?.tracks && res.loadType === "track") {
res = { tracks: [res.data[0]] };
}
if (!res?.tracks && res.loadType === "playlist") {
res = { tracks: [res.data]}
}
if (!res?.tracks && res.loadType === "track") {
res = { tracks: [res.data[0]] };
}
if (!res?.tracks && res.loadType === "playlist") {
res = { tracks: [res.data]}
}
but thats kinda stupid why not have every track into the res.tracks property
absent-sapphire
absent-sapphire•2y ago
the error is in line 4 const track = res.tracks[0]; you are defining the music before checking the loadType this way it is possible that the loadType is something other than "track" or "search" and thus giving error because nothing was found
rival-black
rival-blackOP•2y ago
the error was NOT an error when i was using erela.js
absent-sapphire
absent-sapphire•2y ago
try this code:
const player = bot.nanager.players.get(guildId)
if(!player) player = bot.manager.players.create("createOptions")

var res = await bot.manager.search({ query: search }, member.user.id)

switch(res.loadType) {
case "track":
case "search":
player.queue.add(res.tracks[0])
if (!player.playing && !player.paused) player.play();
break
}
const player = bot.nanager.players.get(guildId)
if(!player) player = bot.manager.players.create("createOptions")

var res = await bot.manager.search({ query: search }, member.user.id)

switch(res.loadType) {
case "track":
case "search":
player.queue.add(res.tracks[0])
if (!player.playing && !player.paused) player.play();
break
}
rival-black
rival-blackOP•2y ago
thank you but I fixed it way easier:
bot.manager.search({ query: search }).then(async (res) => {
if (res.loadType === 'track') res = { tracks: [res.data[0]] };
if (res.loadType === 'playlist') res = { tracks: [res.data] };


// Rest of code
bot.manager.search({ query: search }).then(async (res) => {
if (res.loadType === 'track') res = { tracks: [res.data[0]] };
if (res.loadType === 'playlist') res = { tracks: [res.data] };


// Rest of code
absent-sapphire
absent-sapphire•2y ago
nice!
MEE6
MEE6•2y ago
GG @NedcloarBR, you just advanced to level 2!

Did you find this page helpful?