I am new to nodejs and learning it by building one small project(Blog), facing below mentioned issue while submitting add post form details. I have also added code for addPost.jade and post.js.
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 file which gets rendered fine for GET request
//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 file
var express = require('express');
var router = express.Router();
var mongo = require('mongodb');
var db = require('monk')('localhost/nodeblog');
var multer = require('multer');
var uploads = multer({ dest: './public/images/uploads' });
/* home page blog Post. */
router.get('/add', function(req, res, next) {
var categoryList = db.get('categories');
categoryList.find({},{}, function (err, categoryList) {
/*console.log("Categories"+categories);
categories.forEach(function(category){
console.log(category.title);
});*/
res.render('addPost',{
"title": "Add Post",
"catList" : categoryList
});
});
});
router.post('/add', uploads.single('mainimage'), 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("Title"+title);
console.log("Category"+category);
console.log("body"+body);
console.log("Author"+author);
console.log("Date"+date);
//Check for image field
if(req.files && req.files.mainimage){
console.log('Uploading file...');
var mainImageOrginalName = 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{
console.log('mainImageFile not found....');
var mainImageName = 'noimage.png';
}
//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('/');
}
});
}
res.render('addpost',{
"title": "Add Post"
});
});
module.exports = router;
Please help me in resolving this issues as I am stuck with this.
Thanks
via Ãkshåý Güptã
No comments:
Post a Comment