你好我有问题。我仍然没有发现我的问题。谢谢你帮我这是login.js的代码
var express = require('express');
var router = express.Router();
var db=require('../database');
/* GET users listing. */
router.get('/login', function(req, res, next) {
res.render('login');
});
router.post('/login', function(req, res){
var username = req.body.username;
var password = req.body.password;
var sql='SELECT * FROM users WHERE username =? AND password =?';
db.query(sql, [username, password], function (err, data, fields) {
if(err) throw err
if(data.length>0){
req.session.loggedinUser= true;
req.session.username= username;
res.redirect('/dashboard');
}else{
res.render('login',{alertMsg:"Your username or password is wrong"});
}
})
})
module.exports = router;
这是我的登录页面(
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div class="login-form">
<h3>Login Form</h3>
<p><%=(typeof alertMsg!='undefined')? alertMsg:''%></p>
<form method="post" action="/login">
<input type="text" placeholder="Username" name="username" required">
<input type="password" placeholder="Password" name="password" required">
<button type="submit">Login Now</button>
<a href="/register">New Registration</a>
</div>
</form>
</div>
</body>
</html>
当我输入用户名和密码并单击登录按钮时,它会得到此错误:
TypeError:无法读取undefined的属性(阅读“username')
Image of the error . Click here
如果你能帮助我,我很高兴
1条答案
按热度按时间mnowg1ta1#
似乎问题出在访问**'req.body'对象的' username '属性时,导致TypeError。
您可以尝试在'login.js'文件中添加一个中间件函数来解析请求正文。您可以在'login.js'**文件的顶部添加以下行:
这将使您能够从请求正文访问**'username'和'password'**属性。请确保在进行此更改后重新启动服务器。