Ad

Nodejs Form Cannot Read Property 'docreate' Of Undefined

I'm a new in nodejs, and trying to create a new MVC application with expressjs. I have a html form with "POST" method and when i submit that some error showing. Cannot read property 'docreate' of undefined

TypeError: Cannot read property 'docreate' of undefined
    at exports.save (C:\Users\Hieu Vo\Desktop\hellow\notes\routes\notes.js:13:18)
    at Layer.handle [as handle_request] (C:\Users\Hieu Vo\Desktop\hellow\notes\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\Hieu Vo\Desktop\hellow\notes\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\Hieu Vo\Desktop\hellow\notes\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\Hieu Vo\Desktop\hellow\notes\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\Hieu Vo\Desktop\hellow\notes\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\Users\Hieu Vo\Desktop\hellow\notes\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\Hieu Vo\Desktop\hellow\notes\node_modules\express\lib\router\index.js:275:10)
    at expressInit (C:\Users\Hieu Vo\Desktop\hellow\notes\node_modules\express\lib\middleware\init.js:40:5)
    at Layer.handle [as handle_request] (C:\Users\Hieu Vo\Desktop\hellow\notes\node_modules\express\lib\router\layer.js:95:5)

here is app.js file

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var bodyParser = require('body-parser')
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var notes = require('./routes/notes');
var app = express();
// view engine setup
app.use('/noteadd', notes.add);
app.post('/notesave', notes.save);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(bodyParser.json());
app.use(logger('dev'));
app.use(express.json());
app.post('/', function(request, response){
  console.log(request.body);      // your JSON
   response.send(request.body);    // echo the result back
});
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};
  // render the error page
  res.status(err.status || 500);
  res.render('error');
});
module.exports = app;

I have view file with name noteedit.ejs

<% include top %>
<p value='<%= docreate ? "create" : "update"%>'><%= docreate ? "create" : "update"%></p>
<form method='POST' action='/notesave'>
    <input type='hidden' name='docreate' value='<%= docreate ? "create" : "update"%>'>
    <p>Key: <input type='text' name='notekey' value='<%= note ? notekey : "" %>'></p>
    <p>Title: <input type='text' name='title' value='<%= note ? note.title : "" %>'></p>
    <br/>
    <textarea rows=5 cols=40 name='body'><%= note ? note.body : "" %></textarea>
    <br/>
    <input type='submit' value='Submit' />
</form>
<% include bottom %>

and this is my save function

var notes = require('../models/notes');

exports.add = function(req, res, next) {
    res.render('noteedit', {
        title: "Add a note",
        docreate: true,
        notekey: "",
        note: undefined
    });
}

exports.save = function(req, res, next) {
    if (req.body.docreate === 'create') {
        notes.create(req.body.notekey,
                     req.body.title,
                     req.body.body);
    } else {
        notes.update(req.body.notekey,
                     req.body.title,
                     req.body.body);
    }
    res.redirect('/noteview?key='+req.body.notekey);
}

please help me in my problem, thank you for your help. Thank again.

Ad

Answer

I think you should use 'body-parser'. Please Read this

Ad
source: stackoverflow.com
Ad