exiify
exiify

Reputation: 68

Error: UnhandledPromiseRejectionWarning: TypeError: args2[1].join is not a function

I am trying to make a music bot but whenever i do the search command an error (below) pops up what do i do?

my code is

const search = require('yt-search')

module.exports.run = async (bot, message, args, ops) => {
const prefix = "u!"
const args2 = message.content.slice(prefix.length).trim().split(' ');
    search(args2[1].join(' '), function(err, res) {
        if (err) {
       
            console.log(err)
            return message.channel.send("Sorry, something went wrong")
       
        }
   
   
    let videos = res.videos.slice(0, 10);
 
    let resp = '';
    for (var i in videos) {
        resp += `**[${parseInt(i)+1}]:** \`${videos[i].title}\`\n`;
    }
 
    resp += `\n**Choose between \`1-${videos.length}\``;
 
    message.channel.send(resp)
   
    const filter = m => !isNaN(m.content) && m.content < videos.length+1 && m.content > 0;
 
    const collector = message.channel.createMessageCollector(filter);
 
    collector.videos = videos;
 
    collector.once('collect', function(m) {
        let commandFile = require('./play.js')
        commandFile.run(client, message, [this.videos[parseInt(m.content)-1].url])
    })
 
});
}

and then this error pops up

 UnhandledPromiseRejectionWarning: TypeError: args2[1].join is not a function

I don't know what to do help?

Upvotes: 1

Views: 332

Answers (2)

ESI
ESI

Reputation: 2047

join() is an array method, you've tried to apply it on a lonely element (here is a string).

If you want to send to the search function a new string by concatenating all the elements in an array you have to apply it on the array:

search(args2.join(' '), function(err, res) {...})

if you want to send only the second item from the split array, just send it:

search(args2[1], function(err, res) {...})

Upvotes: 1

Jumshud
Jumshud

Reputation: 1425

I think, you can simply check if args2 is an array:

const args2 = message.content.slice(prefix.length).trim().split(' ');
if (Array.isArray(args2)) {
   //search goes here
}

Upvotes: 1

Related Questions