[英]Node.js undefined properties
I'm trying to create a log in window. 我正在尝试创建一个登录窗口。 My code is simple. 我的代码很简单。
var employees = { '1' : { firstName: '', lastName: '', login: 'qwerty', password: '12345', }, '2' : { login: 'asdfg', password: '12345', }, }; app.post('/main', function(req, res) { if (!req.body) return res.sendStatus(400); console.log(req.body); for (var key in employees) { console.log(key['login']); console.log(key['password']); if ((key.login == req.body.login) && (key.password == req.body.password)) { res.render('main'); } else { app.get('/', function(req,res) { res.send(createIndexPage()); }); }; }; });
Why key.login and key.password return undefined? 为什么key.login和key.password返回未定义? And why else block does not run when if statement is wrong? 如果if语句错误,为什么其他块不会运行?
Look at what the value of key
actually is: 查看key
实际值是什么:
var employees = { '1': { firstName: '', lastName: '', login: 'qwerty', password: '12345', }, '2': { login: 'asdfg', password: '12345', }, }; for (var key in employees) { console.log(key); }
It is the property name (as a string), not the value of the property. 它是属性名称 (作为字符串),而不是属性的值。
console.log(employees[key]['login']);
will give you what you are looking for. 会给您您想要的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.