Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js TypeError:无法读取属性';子串';未定义的_Node.js - Fatal编程技术网

Node.js TypeError:无法读取属性';子串';未定义的

Node.js TypeError:无法读取属性';子串';未定义的,node.js,Node.js,在node.js文件中编写 函数正在返回错误: 返回ACE职业展示标识符 原始Alid示例:46R-002-30BroadJour12\u 01-12\u 11 预期产量:46R-002 /*This function is returning the error: Cannot read property 'substring' of undefined*/ function splitID(originalID){ var aceid = originalID.substring(0,7

在node.js文件中编写

函数正在返回错误:

返回ACE职业展示标识符

原始Alid示例:46R-002-30BroadJour12\u 01-12\u 11

预期产量:46R-002

/*This function is returning the error:
Cannot read property 'substring' of undefined*/
function splitID(originalID){
  var aceid = originalID.substring(0,7);
  return aceid;
}

//1. Get the ace exhibit occupation id for each of them and put it in a parallel array.
for (var row in values) {
  //split the 5th column using our function
  var output = splitID(row[4]);
  var result = getOccupation(output);
  //now we add the split output to our occupation array.
  occupationsToInsert.append(result);
}

错误告诉您确切的问题:
originalID
未定义。在
for
循环中,
行[4]
产生一个未定义的值。验证您的
数组是否包含您期望的值。

如果您可以参考MDN上的文档,它建议不要使用
for…in
在数组上循环,因为它在返回时不会给出一致的值。而是迭代传递给它的相关对象的
可枚举属性

换句话说,
for(var row In values)
不会像预期的那样迭代每一行,而是迭代
值列表的可枚举属性

因此,对于常量数组,只需执行以下操作即可找到可枚举属性

Object.getOwnPropertyNames(values)
将返回以下列表:

["0", "length"]

实际上,您试图访问此数组中不存在的第四个元素,因此该元素未定义,从而导致您观察到的错误。

const values=[[COMM'、'1200'、'INTRO MASS Communications'、'M024'、'46R-002-30BroadJour12_01-12_11']、…];