简体   繁体   中英

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? And why else block does not run when if statement is wrong?

Look at what the value of key actually is:

 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM