在将此标记为重复之前,请理解我所查看的所有答案都不足以解决我的代码问题。我正在尝试修复我正在制作的网络应用程序中的错误,以便从Internet上传文件到家庭服务器。我目前正在研究基本的用户身份验证系统。当我尝试在主页上提交登录表单时,我被重定向到一个有错误的页面,其中一部分表示"无法读取属性'用户名'未定义"。此消息也出现在控制台中。发生了什么,我该如何解决?错误将我引导到家庭控制器的第8行," username = req.body.username"。 App.js
* {margin:0;padding:0;box-sizing:border-box}
html, body {width:100%}
/server/routes.js
var LocalStrategy = require('passport-local').Strategy;
var flash = require("connect-flash");
var passport = require("passport");
var session = require("express-session");
var expressValidator = require("express-validator");
var mongoose = require("mongoose");
var path = require('path');
var express = require('express');
var cookieParser = require('cookie-parser');
var app = express();
var bodyParser = require('body-parser');
var ejs = require('ejs');
var routes = require('./server/routes');
routes(app);
app.set('port', process.env.PORT || 3000);
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(cookieParser());
mongoose.connect("mongodb://localhost:27017/users");
app.listen(app.get('port'), function(){
console.log("server started");
});
var db = mongoose.connection;
app.use(session({
secret: 'yVVma9ga',
saveUninitialized: true,
resave: true
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(expressValidator({
errorFormatter: function(param, msg, value) {
var namespace = param.split('.')
, root = namespace.shift()
, formParam = root;
while(namespace.length) {
formParam += '[' + namespace.shift() + ']';
}
return {
param : formParam,
msg : msg,
value : value
};
}
}));
app.use(flash());
app.use(function(req,res,next) {
res.locals.success_msg = req.flash('success_msg');
res.locals.error_msg = req.flash('error_msg');
res.locals.error = req.flash("error");
next();
});
控制器/ home.js
var express = require("express");
var home = require('../controllers/home');
var client = require('../controllers/client');
module.exports = function(app) {
var router = express.Router();
app.use('/', router);
router.get('/', home.index);
router.get('/client', client.home);
router.post('/', home.login);
}
/views/home.ejs(partials只是基本的html)
module.exports = {
index: function(req, res){
res.render("home");
},
login: function(req, res){
var username = req.body.username;
var password = req.body.password;
req.checkBody('username', 'Username is required').notEmpty();
req.checkBody('password', 'Password is required').notEmpty();
var errors = req.validationErrors();
if(errors){
console.log("YES");
}else{
console.log("NO");
};
}
}
答案 0 :(得分:3)
我敢打赌,在你的app.use调用bodyparser之前,你的路由调用正在发生。尝试将路线移到它们下面。
更改
var routes = require('./server/routes');
routes(app);
//...
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
要
var routes = require('./server/routes');
//...
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
//...
routes(app);
答案 1 :(得分:0)
var routes = require('./server/routes');
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
//...
routes(app);
一切都应放在路线内。否则,您将遇到undefined
错误。