Jade并不是一种容易处理的语言,因为它有适当的缩进而没有,但在这种情况下缩进似乎不是问题而且我有Google-Foo'd错误而且找不到匹配的东西正在这里。
这是我的addpost.jade
文件:
extends layout
block content
h1=title
ul.errors
if errors
each error, i in errors
li.alert.alert-danger #{error.msg}
form(method='post', action='/posts/add', enctype="multipart/form-data")
.form-group
label Title:
input.form-control(name='title', type='text')
.form-group
label Category
select.form-control(name='category')
each category, i in categories
option(value='#{category.title}') #{category.title}
.form-group
label Body
textarea.form-control(name='body', id='body')
.form-group
label Main Image:
input.form-control(name='mainimage', type='file')
.form-group
label Author
select.form-control(name='author')
option(value='Daniel Cortes') Daniel Cortes
option(value='Alejandra Rocha') Alejandra Rocha
input.btn.btn-default(name='submit', type='submit', value='Save')
这是我得到的错误:
addpost.jade:16 14 |标签类别15 | select.form-control(name ='category')> 16 |每个类别,我在类别17 | option(value ='#{category.title}')#{category.title} 18 | .form-group 19 | label Body无法读取未定义的属性“长度”
这是我的posts.js文件:
var express = require('express');
var router = express.Router();
var mongo = require('mongodb');
var db = require('monk')('localhost/nodeblog');
router.get('/add', function(req, res, next){
var categories = db.get('categories');
categories.find({},{},function(err, categories){
res.render('addpost',{
"title": "Add Post",
"categories": categories
});
});
});
router.post('/add', function(req, res, next){
// Get Form Values
var title = req.body.title;
var category = req.body.category;
var body = req.body.body;
var author = req.body.author;
var date = new Date();
if(req.files.mainimage){
var mainImageOriginalName = req.files.mainimage.originalname;
var mainImageName = req.files.mainimage.name;
var mainImageMime = req.files.mainimage.mimetype;
var mainImagePath = req.files.mainimage.path;
var mainImageExt = req.files.mainimage.extension;
var mainImageSize = req.files.mainimage.size;
} else {
var mainImageName = 'noimage.png';
}
// Form Validation
req.checkBody('title','Title field is required').notEmpty();
req.checkBody('body', 'Body field is required');
// Check errors
var errors = req.validationErrors();
if(errors){
res.render('addpost',{
"errors": errors,
"title": title,
"body": body
});
} else {
var posts = db.get('posts');
// Submit to db
posts.insert({
"title": title,
"body": body,
"category": category,
"date": date,
"author": author,
"mainimage": mainimage
}, function(err, post){
if(err){
res.send('There was an issue submitting the post');
} else {
req.flash('success','Post Submitted');
res.location('/');
res.redirect('/');
}
});
}
});
module.exports = router;
我的app.js文件:
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var expressValidator = require('express-validator');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var bodyParser = require('body-parser');
var mongo = require('mongodb');
var db = require('monk')('localhost/nodeblog');
var multer = require('multer');
var flash = require('connect-flash');
var routes = require('./routes/index');
var posts = require('./routes/posts');
var app = express();
app.locals.moment = require('moment');
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// Handle File Uploads & Multipart Data
// app.use(multer({dest: './uploads'}));
app.use(multer({dest:'./uploads/'}).single('singleInputFileName'));
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
// Express Session
app.use(session({
secret: 'secret',
saveUninitialized: true,
resave: true
}));
// Express Validator
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(express.static(path.join(__dirname, 'public')));
// Connect Flash
app.use(flash());
app.use(function (req, res, next) {
res.locals.messages = require('express-messages')(req, res);
next();
});
// Make our db accessible to our router
app.use(function(req, res, next){
req.db = db;
next();
});
app.use('/', routes);
app.use('/posts', posts);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// 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;
答案 0 :(得分:0)
似乎categories
未传递给视图或它不是数组。
您可以添加呈现模板的代码吗?也许我们可以在那里找到问题。