为什么 req.body.title 显示 'undefined'

why req.body.title shows 'undefined'

我是 NODE JS 的新手,正在练习从 PUG 到 NODE JS 服务器的一些 POST 表单。 我有一个简单的表格来更新发布到 mongodb 上的照片标题和描述。当我从 Web 浏览器提交表单时,提交输入作为 'undefined'.

返回到服务器

POST日志中的这两个进程'undefined':(更完整的代码见下文)

  console.log("title:", req.body.title)
  console.log("description", req.body.description)

我尝试改用 PUT。奇怪的是,我以前做过这个,而且很有效。所以我不确定是什么问题...

路由器处理 POST 请求:

//users.js
const express = require('express');
const router = express.Router();
const app = express();
const multer = require('multer');
const photoController = require('../controllers/photoController');
const flash = require('express-flash');
const Photo = require('../models/photoModel');
const upload = multer({
  storage: photoController.storage,
  fileFilter: photoController.imageFilter
});

// flash messaging
router.use(flash());

router.get('/', (req, res, next)=>{
  Photo.find({})
    .then((photos)=>{
      res.render('photos', {
        photos : photos,
        flashMsg: req.flash("fileUploadError")
      });
    })
    .catch((err)=>{
      if (err) {
        res.end("ERROR!");
      }
    });
});


router.get('/:photoid', (req, res, next)=>{
  console.log("finding "+req.params.photoid);
  Photo.findOne({'_id': req.params.photoid})
    .then((photo)=>{
      res.render('updatePhoto', {
        photo: photo,
        flashMsg: req.flash("photoFindError")
      });
    }).catch((err)=>{
      if (err) console.log(err);
    });
});

// I think the error is below!!
router.post('/:photoid', (req, res, next)=>{
  console.log("title:", req.body.title)
  console.log("description", req.body.description)
  Photo.findOne({'_id': req.params.photoid})
    .then((photo)=>{
      var data  = {
         title: req.body.title,
         description: req.body.description
         }
      photo.set(data);
      photo.save().then(()=>{
        res.redirect('/photos');
      });
    })
    .catch((err)=>{
      if (err) console.log(err);
  });
});

PUG 形态:

.row
   .col-md-6.col-md-offset-3
      if flashMsg.length > 0
        .alert.alert-danger <strong>FLASH!</strong>#{flashMsg}
      p Title: #{photo.title} 
      p Description: #{photo.description}
      p Size: #{photo.size} | Filename: #{photo.originalname} | Uploaded: #{photo.createdAt}| Modified: #{photo.updatedAt}
      img(src=photo.imageurl, width="250")
      form(method='POST' action="/photos/"+photo._id enctype="multipart/form-data")
        div.form-group
          label(for='name') Photo Title :
          input#name.form-control(type='text', value=photo.title name='title')
        div.form-group
          label(for='email') Description:
          input#email.form-control(type='text', value=photo.description name='description')
        div.form-group
          label(for='image') Image:
          input#name.form-control(type='hidden', name='_id' value=photo._id)
        button.btn.btn-primary(type='submit') Update Your Photo

感谢您的帮助

您应该将 app.use(express.json()) 中间件添加到您的代码中,以便从您的请求中自动解析 req.body

const express = require('express');
const router = express.Router();
const app = express();
app.use(express.json()); // Here
const multer = require('multer');
const photoController = require('../controllers/photoController');
const flash = require('express-flash');
const Photo = require('../models/photoModel');
const upload = multer({
  storage: photoController.storage,
  fileFilter: photoController.imageFilter
});