Javascript TypeError无法读取属性';长度';未定义的

Javascript TypeError无法读取属性';长度';未定义的,javascript,command-line,Javascript,Command Line,我得到以下错误,不知道如何解决它。我曾尝试在SO上查看类似错误,但不确定它们是否适用。我有一个名为“current”的数组。这个问题可能与作用域有关,但我已经尝试将一些变量的作用域修改为全局等,以使其能够工作,但现在还没有 currentLen = current.length - 1; ^ TypeError: Cannot read property 'length' of undefined at Object.<anonymo

我得到以下错误,不知道如何解决它。我曾尝试在SO上查看类似错误,但不确定它们是否适用。我有一个名为“current”的数组。这个问题可能与作用域有关,但我已经尝试将一些变量的作用域修改为全局等,以使其能够工作,但现在还没有

  currentLen = current.length - 1;
                      ^
TypeError: Cannot read property 'length' of undefined
    at Object.<anonymous> (/Users/user/Desktop/blabla.js:41:23)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:929:3
currentLen=current.length-1;
^
TypeError:无法读取未定义的属性“length”
反对。(/Users/user/Desktop/blabla.js:41:23)
在模块处编译(Module.js:456:26)
在Object.Module.\u extensions..js(Module.js:474:10)
在Module.load(Module.js:356:32)
在Function.Module.\u加载(Module.js:312:12)
位于Function.Module.runMain(Module.js:497:10)
启动时(node.js:119:16)
在node.js:929:3

错误本身非常清楚。当前数组未定义;为了避免出现这些错误,可以检查数组是否存在

currentLen=当前长度和当前长度-1

试试这个:

if (Array.isArray(current)) {
  // current exists and it is an array, therefore the length
  // attribute is guaranteed to exist
}
else {
  // Write some code to handle the case when current is not an array
}

这意味着变量“current”的值
未定义