简体   繁体   English

为什么我收到一个错误说拆分未定义?

[英]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这是应该将我输入控制台的代码并在 discord 服务器上为该机器人打印的代码,但我收到此错误

TypeError: Cannot read property 'split' of undefined TypeError:无法读取未定义的属性“拆分”

You are trying to use split in a string called answer which has not been declared and defined yet.您正在尝试在尚未声明和定义的名为answer的字符串中使用split Swap lines 1 and 2 according to:根据以下交换第 1 行和第 2 行:

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

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.在您的情况下,这可能意味着名为 answer 的变量未定义,但如果不知道该变量是如何创建的,则无法确定。

Did you try debug the javascript source from a browser?您是否尝试从浏览器调试 javascript 源? You can stop in that point and see if your variable has a value or not.您可以停在这一点上,看看您的变量是否有值。

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')
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM