user13368701
user13368701

Reputation:

Why do I get an error saying split undefined?

var anssep = answer.split(" ")
var answer = (" ")
if(anssep[0] == "send"){
  var toSend = answer.replace((anssep[0]+" "), "")
  bot.channels.cache.get("701888561640636510").send(toSend)

that is the code that should take what i type into console and print it on the discord server for this bot but i get this error

TypeError: Cannot read property 'split' of undefined

Upvotes: 0

Views: 947

Answers (3)

Michael Nelles
Michael Nelles

Reputation: 6032

First establish that it is not undefined then ensure it is a string:

if(answer !== undefined){
  var anssep = answer.toString().split(" ")
} else {
 console.log('answer is undefined')
}

Upvotes: 0

AndD
AndD

Reputation: 2701

That error usually means you are trying to call a method of something undefined. In your case, it probably means the variable called answer is undefined but there's no way to be sure without knowing how that variable gets created.

Did you try debug the javascript source from a browser? You can stop in that point and see if your variable has a value or not.

Upvotes: 1

vtolentino
vtolentino

Reputation: 784

You are trying to use split in a string called answer which has not been declared and defined yet. Swap lines 1 and 2 according to:

var answer = (" ")
var anssep = answer.split(" ")

Upvotes: 2

Related Questions