我得到上面的错误我只是弄清楚为什么?之前我看到过这个错误,可能是因为变量不匹配。
我的路线.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.hasOwnProperty('file')) {
console.log('Uploading File...');
//File info
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 {
//Set a Default Image
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('post');
//Submit to DB
posts.insert({
'title': title,
'body': body,
'category': category,
'date': date,
'author': author,
'mainimage': mainImageName
}, function(err, post){
if(err){
res.send('There was an issue submitting the post');
} else {
req.flash('succes', 'post submitted');
res.location('/');
res.redirect('/');
}
});
}
});
module.exports = router;
我的.jade文件:
extends layout
block content
h1=title
ul.errors
if error
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='Marko') Marko
option(value='John Do') John Do
input.btn.btn-default(name='submit', type='submit', value='Save')
script(src='/ckeditor/ckeditor.js')
script
| CKEDITOR.replace('body');
完整的错误日志:
TypeError: C:\projects\nodeblog\views\addpost.jade:16
14| label Category
15| select.form-control(name='category')
> 16| each category, i in categories
17| option(value='#{category.title}') #{category.title}
18| .form-group
19| label Body
Cannot read property 'length' of undefined
at eval (eval at <anonymous> (C:\projects\nodeblog\node_modules\jade\lib\index.js:218:8), <anonymous>:204:31)
at eval (eval at <anonymous> (C:\projects\nodeblog\node_modules\jade\lib\index.js:218:8), <anonymous>:241:4)
at eval (eval at <anonymous> (C:\projects\nodeblog\node_modules\jade\lib\index.js:218:8), <anonymous>:374:22)
at res (C:\projects\nodeblog\node_modules\jade\lib\index.js:219:38)
at Object.exports.renderFile (C:\projects\nodeblog\node_modules\jade\lib\index.js:380:38)
at Object.exports.renderFile (C:\projects\nodeblog\node_modules\jade\lib\index.js:370:21)
at View.exports.__express [as engine] (C:\projects\nodeblog\node_modules\jade\lib\index.js:417:11)
at View.render (C:\projects\nodeblog\node_modules\express\lib\view.js:126:8)
at tryRender (C:\projects\nodeblog\node_modules\express\lib\application.js:639:10)
at EventEmitter.render (C:\projects\nodeblog\node_modules\express\lib\application.js:591:3)
at ServerResponse.render (C:\projects\nodeblog\node_modules\express\lib\response.js:961:7)
at C:\projects\nodeblog\routes\posts.js:48:9
at Layer.handle [as handle_request] (C:\projects\nodeblog\node_modules\express\lib\router\layer.js:95:5)
at next (C:\projects\nodeblog\node_modules\express\lib\router\route.js:131:13)
at Route.dispatch (C:\projects\nodeblog\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\projects\nodeblog\node_modules\express\lib\router\layer.js:95:5)
如果有人帮忙,我会很感激。
答案 0 :(得分:0)
请控制错误对象,并在下面的代码中获取类别时检查是否有类别。
router.get('/add', function(req, res, next) {
var categories = db.get('categories');
categories.find({}, {}, function(err, categories){
** console error and check categories type should be an array**
res.render('addpost', {
'title': 'Add Post',
'categories': categories
});
});
})