【问题标题】:Jade cannot read property lengthJade 无法读取属性长度
【发布时间】:2017-01-15 09:15:03
【问题描述】:

我的 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

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

【问题讨论】:

    标签: javascript node.js pug


    【解决方案1】:
    if(errors){
        res.render('addpost', {
            'errors'    : errors,
            'title'     : title,
            'body'      : body
        });
    }
    

    据我了解,如果在您的帖子中出现错误,您会呈现提供错误的相同视图。不过,您没有提供categories,所以当 Jade(Pug) 呈现视图时它是未定义的。我想这就是错误的来源......?

    【讨论】:

    • 感谢您的建议,但这没有帮助。
    • 你确定吗? codepen.io/anon/pen/QdKyEv // 没有类别 -> 错误 codepen.io/anon/pen/OWRMXN // 类别数组 -> 渲染
    • 是的,它正在渲染,但提交时弹出相同的错误。
    • 各位,我还有同样的问题吗?你能帮帮我吗?
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 2013-03-21
      相关资源
      最近更新 更多