玉无法读取属性长度

时间:2017-01-15 09:15:03

标签: javascript node.js pug

我将post.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();

    console.log(req.body);
    if(req.file){
        var mainImageOriginalName   = req.file.originalname;
        var mainImageName           = req.file.filename;
        var mainImageSize           = req.file.size;
        var mainImageMime           = req.file.mimetype;
        var mainImageExt            = req.file.extension;
        var mainImagePath           = req.file.path;
    } else {
        var mainImageName   = 'noimage.png';
    }

    //form validation
    req.checkBody('title', 'Title field is required').notEmpty();
    req.checkBody('body', 'Body field is required').notEmpty();

    //check errors
    var errors = req.validationErrors();

    if(errors){
        res.render('addpost', {
            'errors'    : errors,
            'title'     : title,
            'body'      : body
            'categories': category
        });
    } else {
        var posts = db.get('posts');

        //submit to db
        posts.insert({
            'title': title,
            'body': body,
            'category': category,
            'date': date,
            'author': author,
            'image': mainImageName
        }, 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;

和我的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='image', type='file')
        .form-group
            label Author
            select.form-control(name='author')
                option(value='Shehzad Shaikh') Shehzad Shaikh
                option(value='John Doe') John Doe
        input.btn.btn-default(name='submit', type='submit', value='Save')

        script(src='/ckeditor/ckeditor.js')
        script
            | CKEDITOR.replace('body');

当我提交表单时,它会给我一个错误,上面写着

nodeblog/views/addpost.jade:20 18| label Category 19| select.form-control(name='category') > 20| each category, i in categories 21| option(value='#{category.title}') #{category.title} 22| .form-group 23| label Body Cannot read property 'length' of undefined

我查了变量名,对我来说很好看。究竟出了什么问题? 此外,如果选择框出现问题,它会正确呈现值,但是在提交表单时会出现此问题。

2 个答案:

答案 0 :(得分:0)

if(errors){
    res.render('addpost', {
        'errors'    : errors,
        'title'     : title,
        'body'      : body
    });
}

根据我的理解,如果您的帖子中有错误,您将呈现相同的视图,提供错误。尽管如此,你没有提供categories所以当Jade(Pug)呈现视图时它是未定义的。我想那是错误来自......?

答案 1 :(得分:0)

我遇到了同样的问题。在jade文件中添加if条件解决了我的问题。

    .form-group
        label Category
        select.form-control(name='category')
            if categories
                each category,i in categories
                    option(value='#{category.title}') #{category.title}