这是一个奇怪的错误,因为它看起来是错误的,错误是:
TypeError: Cannot set property 'mark' of undefined
at /home/ubuntu/workspace/tests/app.js:194:36
at Layer.handle [as handle_request](/home/ubuntu/workspace/tests/node_modules/express/lib/router/layer.js:95:5)
at next (/home/ubuntu/workspace/tests/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/home/ubuntu/workspace/tests/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/ubuntu/workspace/tests/node_modules/express/lib/router/layer.js:95:5)
at /home/ubuntu/workspace/tests/node_modules/express/lib/router/index.js:277:22
at Function.process_params (/home/ubuntu/workspace/tests/node_modules/express/lib/router/index.js:330:12)
at next (/home/ubuntu/workspace/tests/node_modules/express/lib/router/index.js:271:10)
at SessionStrategy.module.exports.strategy.pass (/home/ubuntu/workspace/tests/node_modules/passport/lib/middleware/authenticate.js:325:9)
at SessionStrategy.authenticate (/home/ubuntu/workspace/tests/node_modules/passport/lib/strategies/session.js:71:10)
at attempt (/home/ubuntu/workspace/tests/node_modules/passport/lib/middleware/authenticate.js:348:16)
at authenticate (/home/ubuntu/workspace/tests/node_modules/passport/lib/middleware/authenticate.js:349:7)
at Layer.handle [as handle_request] (/home/ubuntu/workspace/tests/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/home/ubuntu/workspace/tests/node_modules/express/lib/router/index.js:312:13)
at /home/ubuntu/workspace/tests/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/home/ubuntu/workspace/tests/node_modules/express/lib/router/index.js:330:12)
创建此错误的代码是:
for(var i = 0;i < req.body.numOfMethods;i++)
{
for(var x = 0;x< numOfParts[i];x++)
{
methodsArray[i][x].mark=parts[i][x].mark;
methodsArray[i][x].content=parts[i][x].content;
}
}
然而,当我在此文本下运行代码时
console.log("numOfParts[i] = "+numOfParts[0]);
console.log("numOfParts[i] = "+numOfParts[1]);
console.log("numOfMethods = "+req.body.numOfMethods);
for(var i = 0;i < req.body.numOfMethods;i++)
{
for(var x = 0;x< numOfParts[i];x++)
{
console.log("i = "+i);
console.log("x = "+x);
console.log("parts[i][x].mark = "+parts[i][x].mark);
console.log("parts[i][x].content = "+parts[i][x].content);
}
}
打印出来:
numOfParts[i] = 3
numOfParts[i] = 2
numOfMethods = 2
i = 0
x = 0
parts[i][x].mark = 2
parts[i][x].content = test
i = 0
x = 1
parts[i][x].mark = 2
parts[i][x].content = test
i = 0
x = 2
parts[i][x].mark = 2
parts[i][x].content = test
i = 1
x = 0
parts[i][x].mark = 3
parts[i][x].content = test
i = 1
x = 1
parts[i][x].mark = 3
parts[i][x].content = test
这表明methodsArray[i][x].mark
未设置为未定义的值。所以最终在这里我很想知道应该怎样做才能解决这个错误。
这是我定义相关数组的地方(如果我的错误存在于此代码中)
var methodsArray=[[{mark:Number,content:String}]];
var numOfParts=[req.body.m1parts,req.body.m2parts,req.body.m3parts,req.body.m4parts];
var parts=[
[
{mark:req.body.m1p1mark,content:req.body.m1p1content},{mark:req.body.m1p2mark,content:req.body.m1p2content},
{mark:req.body.m1p3mark,content:req.body.m1p3content},{mark:req.body.m1p4mark,content:req.body.m1p4content},
{mark:req.body.m1p5mark,content:req.body.m1p5content},{mark:req.body.m1p6mark,content:req.body.m1p6content},
{mark:req.body.m1p7mark,content:req.body.m1p7content},{mark:req.body.m1p8mark,content:req.body.m1p8content},
{mark:req.body.m1p9mark,content:req.body.m1p9content},{mark:req.body.m1p10mark,content:req.body.m1p10content}
],
[
{mark:req.body.m2p1mark,content:req.body.m2p1content},{mark:req.body.m2p2mark,content:req.body.m2p2content},
{mark:req.body.m2p3mark,content:req.body.m2p3content},{mark:req.body.m2p4mark,content:req.body.m2p4content},
{mark:req.body.m2p5mark,content:req.body.m2p5content},{mark:req.body.m2p6mark,content:req.body.m2p6content},
{mark:req.body.m2p7mark,content:req.body.m2p7content},{mark:req.body.m2p8mark,content:req.body.m2p8content},
{mark:req.body.m2p9mark,content:req.body.m2p9content},{mark:req.body.m2p10mark,content:req.body.m2p10content}
],
[
{mark:req.body.m3p1mark,content:req.body.m3p1content},{mark:req.body.m3p2mark,content:req.body.m3p2content},
{mark:req.body.m3p3mark,content:req.body.m3p3content},{mark:req.body.m3p4mark,content:req.body.m3p4content},
{mark:req.body.m3p5mark,content:req.body.m3p5content},{mark:req.body.m3p6mark,content:req.body.m3p6content},
{mark:req.body.m3p7mark,content:req.body.m3p7content},{mark:req.body.m3p8mark,content:req.body.m3p8content},
{mark:req.body.m3p9mark,content:req.body.m3p9content},{mark:req.body.m3p10mark,content:req.body.m3p10content}
],
[
{mark:req.body.m4p1mark,content:req.body.m4p1content},{mark:req.body.m4p2mark,content:req.body.m4p2content},
{mark:req.body.m4p3mark,content:req.body.m4p3content},{mark:req.body.m4p4mark,content:req.body.m4p4content},
{mark:req.body.m4p5mark,content:req.body.m4p5content},{mark:req.body.m4p6mark,content:req.body.m4p6content},
{mark:req.body.m4p7mark,content:req.body.m4p7content},{mark:req.body.m4p8mark,content:req.body.m4p8content},
{mark:req.body.m4p9mark,content:req.body.m4p9content},{mark:req.body.m4p10mark,content:req.body.m4p10content}
]
];
答案 0 :(得分:0)
var methodsArray=[[{mark:Number,content:String}]];
除了methodsArray[0][0]
之外没有任何其他内容。
methodsArray[i][x]
(甚至methodsArray[i]
)是undefined
(i===0
和x===0
除外[除了数组方法之外,例如i===0
和x==="push"
,但那还不合适。)。
如测试输出所示,您的循环正在访问methodsArray[0][0]
以外的其他内容并尝试设置属性。
如果您愿意,可以随时随地创建元素:
var methodsArray = [];
for(var i = 0; i < req.body.numOfMethods; i++)
{
methodsArray.push([]);
for(var x = 0; x< numOfParts[i]; x++)
{
methodsArray.push({
mark: parts[i][x].mark,
content: parts[i][x].content
});
}
}
创建methodsArray
时,这只是一次。
另外注意,我意识到你可能会写一些Typescript(或者可能是Flow?)。类型注释不使用=
,而是使用:
,因此需要var methodsArray:{mark:Number,content:String}[][];
或var methodsArray:Array<Array<{ mark:number, content:string }>>;
。在Javascript中,var methodsArray=[[{ mark:number, content:string }]];
是一个包含一个元素的数组,一个数组又有一个元素是一个对象,其中属性mark
是Number
构造函数,content
是String
构造函数。