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
deep-jade
deep-jade•3y ago
your code?
sensitive-blue
sensitive-blueOP•3y 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
ratty-blush
ratty-blush•3y 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
sensitive-blue
sensitive-blueOP•3y ago
the error was NOT an error when i was using erela.js
ratty-blush
ratty-blush•3y 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
}
sensitive-blue
sensitive-blueOP•3y 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
ratty-blush
ratty-blush•3y ago
nice!
MEE6
MEE6•3y ago
GG @NedcloarBR, you just advanced to level 2!

Did you find this page helpful?