2017-04-14 45 views
1

我是nodejs的新手,通過構建一個小型項目(Blog)來學習它,當提交添加帖子表單細節時,面臨下面提到的問題。我還添加了addPost.jade和post.js的代碼。其獲取呈現罰款GET請求無法讀取undefined玉的屬性'長度'

TypeError: E:\Web projects\NodeJs projects\nodeBlog\views\addpost.jade:19 
    17|    label Category: 
    18|    select.form-control(name='category') 
> 19|     each cat, i in catList 
    20|      option(value = '#{cat.title}') #{cat.title} 
    21|   .form-group 
    22|    label Body: 

Cannot read property 'length' of undefined 
    at eval (eval at <anonymous> (E:\Web projects\NodeJs projects\nodeBlog\node_modules\jade\lib\index.js:218:8), <anonymous>:210:31) 
    at eval (eval at <anonymous> (E:\Web projects\NodeJs projects\nodeBlog\node_modules\jade\lib\index.js:218:8), <anonymous>:247:4) 
    at eval (eval at <anonymous> (E:\Web projects\NodeJs projects\nodeBlog\node_modules\jade\lib\index.js:218:8), <anonymous>:383:22) 
    at res (E:\Web projects\NodeJs projects\nodeBlog\node_modules\jade\lib\index.js:219:38) 
    at Object.exports.renderFile (E:\Web projects\NodeJs projects\nodeBlog\node_modules\jade\lib\index.js:380:38) 
    at Object.exports.renderFile (E:\Web projects\NodeJs projects\nodeBlog\node_modules\jade\lib\index.js:370:21) 
    at View.exports.__express [as engine] (E:\Web projects\NodeJs projects\nodeBlog\node_modules\jade\lib\index.js:417:11) 
    at View.render (E:\Web projects\NodeJs projects\nodeBlog\node_modules\express\lib\view.js:126:8) 
    at tryRender (E:\Web projects\NodeJs projects\nodeBlog\node_modules\express\lib\application.js:639:10) 
    at EventEmitter.render (E:\Web projects\NodeJs projects\nodeBlog\node_modules\express\lib\application.js:591:3) 
    at ServerResponse.render (E:\Web projects\NodeJs projects\nodeBlog\node_modules\express\lib\response.js:960:7) 
    at E:\Web projects\NodeJs projects\nodeBlog\routes\post.js:89:6 
    at Layer.handle [as handle_request] (E:\Web projects\NodeJs projects\nodeBlog\node_modules\express\lib\router\layer.js:95:5) 
    at next (E:\Web projects\NodeJs projects\nodeBlog\node_modules\express\lib\router\route.js:131:13) 
    at Immediate.<anonymous> (E:\Web projects\NodeJs projects\nodeBlog\node_modules\multer\lib\make-middleware.js:53:37) 
    at runCallback (timers.js:655:20) 

addPost.jade文件

//Created by Sinner on 11-Apr-17. 

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 cat, i in catList 
         option(value = '#{cat.title}') #{cat.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='Akshay Gupta') Akshay Gupta 
        option(value='Riddhi Tandon') Riddhi Tandon 
      input.btn.btn-default(name='Submit', type='submit', value='Save') 
      script(src='/ckeditor/ckeditor.js') 
      script CKEDITOR.replace('body') 

post.js文件

​​

請幫我解決這個問題,因爲我堅持這一點。

PS:也帖子越來越添加

感謝

回答

1

的問題是在這部分代碼。

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

 
    var errors = req.validationErrors(); 
 

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

 
     // add it 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('success', "Post Submitted"); 
 
       req.location('/'); 
 
       req.redirect('/'); 
 
      } 
 
     }); 
 
    } 
 
    
 
    //To be specific this one 
 
    res.render('addpost',{ 
 
     "title": "Add Post" 
 
    });

你的代碼res.render()最後一個塊將posts.insert();之前運行,因爲異步處理的。哪些沒有catList。

更新:

您可以檢查是否有catList在你的玉石,像你已檢查了錯誤的解決這個問題。如下圖所示:

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

另一件事是,我不明白你在去年有res.render(),而你必須在你的代碼覆蓋了所有的情況。

+0

你能告訴我該如何解決這個問題? –

+0

我已經找到了問題,感謝您的幫助。 –

+0

我已將註釋碼 //具體爲這一個 res.render('addpost',{ 「title」:「Add Post」 }); 和 req.location('/'); req.redirect( '/');這應該是res.location('/')和res.redirect('/') –

相關問題