我正在检查字符串是否是数组中另一个字符串的子字符串。这是我的index.js的一部分:
var state = [];
router.post('/doquery', function(req, res) {
var input = req.body.input;
var tempState = [];
var i = 0;
console.log(input);
console.log(state);
console.log(state.length);
while (i < state.length) {
if ( ( ((state[i].name).indexOf(input)) !== -1 ) ||
( ((state[i].city).indexOf(input)) !== -1 ) ||
( ((state[i].country).indexOf(input)) !== -1 ) ||
( ((state[i].birthday).indexOf(input)) !== -1 ) ||
( ((state[i].email).indexOf(input)) !== -1 )||
( ((state[i].phone).indexOf(input)) !== -1 ) ) {
tempState.push(state[i]);
}
i++;
}
res.render('doquery', {title: 'Doquery', tempState: tempState});
});
我收到此错误消息:无法读取属性&#39;长度&#39;未定义的。我明白这意味着状态没有定义。但我确实定义了它。有关如何修复它的任何建议吗?
编辑:如果它有任何区别,我的代码中也有这个:
router.post('/add', function(req, res) {
var obj = { name: req.body.name,
city: req.body.city,
country: req.body.country,
birthday: req.body.birthday,
email: req.body.email,
phone: req.body.phone };
state.push(obj);
res.render('add', { title: 'Person just added', item: obj });
});
州有var obj。