如何修复“ TypeError:无法读取未定义的属性'name'” NodeJs

时间:2019-01-31 17:02:17

标签: javascript node.js express passport.js

当我尝试请求设置var的表单信息时,出现TypeError

VS代码上的NodeJS服务器

这是我的app.js代码(不完整)

var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var exphbs = require('express-handlebars');
var expressValidator = require('express-validator');
var bodyParser = require('body-parser');
var flash = require('connect-flash');
var session = require('express-session');
var passport = require('passport');
var LocalStrategy = require('passport-local'),Strategy;
var mongo = require('mongodb');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/loginapp', { useNewUrlParser: true });
var db = mongoose.connection;

var routes = require('./routes/index');
var users = require('./routes/users');

// APP INIT
var app = express();

// VIEW ENGINE
app.set('views', path.join(__dirname,'views'));
app.engine('handlebars', exphbs({ defaultLayout:'layout' }));
app.set('view engine','handlebars');

// BODYPARSER
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended:false }));
app.use(cookieParser());

app.use('/', routes);
app.use('/users', users);

users.js文件

var express = require('express');
var router = express.Router();

// REGISTER
router.get('/register', (req, res) => {
    res.render('register');
});

// LOGIN
router.get('/login', (req, res) => {
    res.render('login');
});

// REGISTER
router.post('/register', (req, res) => {
    var name = req.body.name;
    console.log(name);
});

module.exports = router;

注册文件(.handlebars html)

<h2 class="page-header">Register</h2>
<form method="post" action="/users/register">
    <div class="form-group">
        <label>Name</label>
        <input type="text" class="form-control" placeholder="Name" name="name">
    </div>
    <div class="form-group">
        <label>Username</label>
        <input type="text" class="form-control" placeholder="Username" name="username">
    </div>
    <div class="form-group">
        <label>Email</label>
        <input type="email" class="form-control" placeholder="Email" name="email">
    </div>
    <div class="form-group">
        <label>Password</label>
        <input type="password" class="form-control" placeholder="Password" name="password">
    </div>
    <div class="form-group">
        <label>Confirm Password</label>
        <input type="password" class="form-control" placeholder="Password" name="password2">
    </div>
    <button type="submit" class="btn btn-default">Submit</button>
</form>

当我尝试提交错误时。

TypeError:无法读取未定义的属性“名称”     在router.post(C:\ Users \ Corentin \ Documents \ loginapp \ routes \ users.js:16:25)     在Layer.handle上(作为handle_request)(C:\ Users \ Corentin \ Documents \ loginapp \ node_modules \ express \ lib \ router \ layer.js:95:5)     在下一个(C:\ Users \ Corentin \ Documents \ loginapp \ node_modules \ express \ lib \ router \ route.js:137:13)     在Route.dispatch(C:\ Users \ Corentin \ Documents \ loginapp \ node_modules \ express \ lib \ router \ route.js:112:3)     在Layer.handle上(作为handle_request)(C:\ Users \ Corentin \ Documents \ loginapp \ node_modules \ express \ lib \ router \ layer.js:95:5)     在C:\ Users \ Corentin \ Documents \ loginapp \ node_modules \ express \ lib \ router \ index.js:281:22     在Function.process_params(C:\ Users \ Corentin \ Documents \ loginapp \ node_modules \ express \ lib \ router \ index.js:335:12)     在下一个(C:\ Users \ Corentin \ Documents \ loginapp \ node_modules \ express \ lib \ router \ index.js:275:10)     在Function.handle(C:\ Users \ Corentin \ Documents \ loginapp \ node_modules \ express \ lib \ router \ index.js:174:3)     在路由器上(C:\ Users \ Corentin \ Documents \ loginapp \ node_modules \ express \ lib \ router \ index.js:47:12)     在Layer.handle上(作为handle_request)(C:\ Users \ Corentin \ Documents \ loginapp \ node_modules \ express \ lib \ router \ layer.js:95:5)     在trim_prefix(C:\ Users \ Corentin \ Documents \ loginapp \ node_modules \ express \ lib \ router \ index.js:317:13)     在C:\ Users \ Corentin \ Documents \ loginapp \ node_modules \ express \ lib \ router \ index.js:284:7     在Function.process_params(C:\ Users \ Corentin \ Documents \ loginapp \ node_modules \ express \ lib \ router \ index.js:335:12)     在下一个(C:\ Users \ Corentin \ Documents \ loginapp \ node_modules \ express \ lib \ router \ index.js:275:10)     在C:\ Users \ Corentin \ Documents \ loginapp \ node_modules \ express \ lib \ router \ index.js:635:15

非常感谢人们的帮助,我花了很多时间解决这个问题

0 个答案:

没有答案