我是keystonejs的新手(使用最新的4)。基于demo app contact form code,我创建了一个表单来添加2个日期。我的问题是,在表单提交时,我收到500错误"无法读取属性'预订来自'未定义" ...你可以帮我跟踪这个吗?谢谢!
我的代码是:
模板/视图/ booking.jade
extends ../layouts/default
block content
form(method='post').form-horizontal
input(type='hidden', name='action', value='booking.create')
.form-group
label.col-sm-2.text-left.control-label From
.col-sm-10
input(type='date' name='bookedFrom' value=formData.bookedFrom required placeholder="2018-01-01").form-control
.form-group
label.col-sm-2.control-label To
.col-sm-10
input(type='date' name='bookedTo' value=formData.bookedTo required placeholder="2018-12-31").form-control
.form-group
button(type='submit').btn.btn-primary Book
路由/视图/ booking.js
var keystone = require('keystone');
var Booking = keystone.list('Booking');
exports = module.exports = function (req, res) {
var view = new keystone.View(req, res);
var locals = res.locals;
// Set locals
locals.section = 'booking';
locals.formData = req.body || {};
locals.validationErrors = {};
locals.bookingSubmitted = false;
view.on('post', { action: 'booking.create' }, function (next) {
var newBooking = new Booking.model({
bookedFrom: locals.FormData.bookedFrom,
bookedTo: locals.FormData.bookedTo
});
var updater = newBooking.getUpdateHandler(req);
updater.process(req.body, {
fields: 'bookedFrom, bookedTo',
flashErrors: true
}, function (err,res) {
if (err) {
locals.validationErrors = err.errors;
} else {
locals.bookingSubmitted = true;
}
next();
});
});
// Render the view
view.render('booking');
};
模型/ Booking.js
var keystone = require('keystone');
var Types = keystone.Field.Types;
var Booking = new keystone.List('Booking', { track: true });
/**
* Booking Model
* =============
*/
Booking.add({
bookedFrom: { type: Types.Date, index: true },
bookedTo: { type: Types.Date, index: true },
});
Booking.track = true;
Booking.defaultSort = '-createdAt';
Booking.defaultColumns = 'createdBy, bookedFrom, bookedTo';
Booking.register();
路由/ index.js
var keystone = require('keystone');
var middleware = require('./middleware');
var importRoutes = keystone.importer(__dirname);
// Common Middleware
keystone.pre('routes', middleware.initLocals);
keystone.pre('render', middleware.flashMessages);
// Import Route Controllers
var routes = {
views: importRoutes('./views'),
};
// Setup Route Bindings
exports = module.exports = function (app) {
// Views
app.get('/', routes.views.index);
app.all('/booking', middleware.requireUser, routes.views.booking);
};
答案 0 :(得分:0)
您的变量名称中有拼写错误。您实例化locals.formData
变量,但稍后将其称为locals.FormData
。后一个变量未定义,导致500错误。重命名模型创建中引用的变量。
var newBooking = new Booking.model({
bookedFrom: locals.formData.bookedFrom,
bookedTo: locals.formData.bookedTo
});