Node.js表单无法读取未定义的属性“ docreate”

时间:2018-10-07 15:05:56

标签: json node.js express model-view-controller

我是nodejs的新手,正在尝试使用expressjs创建新的MVC应用程序。我有一个带有“ POST”方法的html表单,当我提交显示的某些错误时。 无法读取未定义的属性“ docreate”

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)

这是app.js文件

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;

我有一个名为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 %>

这是我的保存功能

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);
}

请帮助我解决我的问题,谢谢您的帮助。 再次感谢。

2 个答案:

答案 0 :(得分:0)

在代码的这一部分:

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);
}

您正在尝试获取req.body的属性docreate,并且错误表明该属性不存在。在检查它是否等于'create'之前,请确保已定义它

答案 1 :(得分:0)

我认为您应该使用“ body-parser”。请阅读this