我正在尝试使用Sequelize从表单中保存一些数据。当我发布表单时,我收到错误:
TypeError:无法将undefined或null转换为object
server.js
var routes = require('./routes/index');
var app = express();
app.use(logger('dev'));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(passport.initialize());
app.use(passport.session());
app.use(bodyParser.urlencoded({extended: true}));
app.use(flash());
//template engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use('/', routes);
// app.use('/user', user);
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
WWW
#!/usr/bin/env mode
var debug = require('debug')('entreeapp')
var app = require('../server');
var models = require('../model');
app.set('port', process.env.PORT || 3000);
//server running
models.sequelize.sync({
force: true
}).then(function () {
var server = app.listen(app.get('port'), function(){
console.log('The magic is happening on port '+server.address().port);
});
});
index.js route
router.post('/register', function(req, res){
var name = req.body.name;
var lastName = req.body.lastName;
var email = req.body.email;
var gender = req.body.gender;
var cellNumber = req.body.cellNumber;
var password = req.body.password;
models.User.build({name: name, lastName: lastName, email: email, gender: gender, cellNumber: cellNumber, password: password})
.save();
});
虽然有从表格中返回的值。附件是intellij调试器的截图。